开发者

C# directory to "play with"

Simple question here. I have a C# program which needs to stores some files onto the hard drive, but I don't need them to be anywhere useful to the end-user, only somewhere that the program can read/write from.

开发者_JS百科Is there a directory that I can reference programmatically to be my "filespace playground" - that is, that I can read/write freely to and from?

EDIT: Also, if I use a temp directory, how long are the files guaranteed to be there? I don't want the them to disappear while my program is still running!


I would use the Application Data Directory. You can get to it using something like:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

It is the prefered location in windows for application specific data and it is generally hidden from the user unless they would like to go and find it.


You can use the system temporary directory which you can get with:

string tmpDir = System.IO.Path.GetTempPath();

If you want, you can create a subfolder under there. The temp folder is great for files that you don't care about. If you want to keep the files you can use the ApplicationData folder as Tim C and Graham Miller suggested.


I think you want the application data directory:

var appplicationDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);


It sounds like you're looking for isolated storage:

Isolated storage is a data storage mechanism that provides isolation and safety by defining standardized ways of associating code with saved data. Standardization provides other benefits as well. Administrators can use tools designed to manipulate isolated storage to configure file storage space, set security policies, and delete unused data. With isolated storage, your code no longer needs unique paths to specify safe locations in the file system, and data is protected from other applications that only have isolated storage access. Hard-coded information that indicates where an application's storage area is located is unnecessary.


Use the system Temp directory:

System.IO.Path.Combine( System.IO.Path.GetTempPath(), "your app name" )

The temp directory is automatically cleaned up by the system (usually by hard disk cleanup) so it's usually the best bet for storing random files that are only needed while the app is running and don't need to stick around.

C# directory to "play with"

If you need a more permanent solution that is storing user data files, use the AppData folder as suggested by other folks.


C:\TEMP?

seriously, no, there is no build-in sandbox playground. You have to chose/create a directory which fits your needs.


This is normally the AppData folder. It's in a user specific directory tree, but not in a place that normal users go.


Usually, app-specific data is stored in locations specified by of the SpecialFolder enumeration. You can use ApplicationData for per-use app-specific data, or LocalApplicationData for per-computer variant:

var playground = Environment.GetFolderPath(
    Environment.SpecialFolder.ApplicationData));


Isolated Storage may work for you.

http://msdn.microsoft.com/en-us/library/bdts8hk0%28VS.95%29.aspx

or,

http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/53def6a4-076c-43f8-86e9-e223d99396a0


The base class library offers you a class to manage a set of temporary files. It is well hidden in the System.CodeDom namespace but nonetheless it can be useful in other contexts as well:

TempFileCollection

The following sample shows how the TempFileCollection class can be used.

using System;
using System.CodeDom.Compiler;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        TempFileCollection tfc = new TempFileCollection(Path.GetTempPath());

        // add a temporary text file
        string filename1 = tfc.AddExtension("txt");

        // add another file with a fully specified name 
        // this file will not automatically be deleted
        string filename2 = Path.Combine(tfc.TempDir, "mycustomfile.txt");
        tfc.AddFile(filename2, true);
        Console.WriteLine(tfc.Count);
        // Create and use the test files.
        File.WriteAllText(filename1, "Hello World.");
        File.WriteAllText(filename2, "Hello again.");
        tfc.Delete();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜