YosiFZ
YosiFZ

Reputation: 7900

Save List to file on disk

I build WPF program , And i use this code to save a List of objects to a File:

    var list = new ArrayList();
    list.Add("item1");
    list.Add("item2");

    // Serialize the list to a file
    var serializer = new BinaryFormatter();
    using (var stream = File.OpenWrite("test.dat"))
    {
        serializer.Serialize(stream, list);
    }

And my problem is where to save this file on the disk. i read that i can't use the ProgramFiles Folder because sometimes only the admin user can save to this folder files.

There is any universal folder that i can use to save files?

Upvotes: 2

Views: 1757

Answers (2)

Andy
Andy

Reputation: 30418

Is this data internal to your program, or user generated? If the data is internal, you probably want to use the Application Data folder. If it's user generated, you should probably default to My Documents, but let the user to decide where to save it.

You can call Environment.GetFolderPath() to get the location of these special folders.

Upvotes: 2

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25435

I'd save it to Application Data. You can get it's path using

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

Upvotes: 4

Related Questions