tags:

views:

170

answers:

10

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.

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!

+1  A: 

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.

Jeff Sternal
A: 

C:\TEMP?

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

codymanix
A: 

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

Eclipsed4utoo
A: 

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));
Anton Gogolev
+1  A: 

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.

alt text

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

chaiguy
You can also open it up easily by typing "%Temp%\your app name" into the Run dialog.
chaiguy
The temp folder will not be deleted while your app is running--I think Hard Disk Cleanup only deletes temp files that haven't been accessed for a week or more.
chaiguy
Actually, they are not "automatically" cleaned up - the user has to delete them manually or intentionally run Hard Disk Cleanup. HDC can clean them up "automatically", but a user action is still required first.
GalacticCowboy
Well it's more "automatic" than any other location I can think of. I didn't mean to give the impression it's okay to just throw files in there and forget about them. You (i.e. the programmer) should still clean them up yourself when you're done with them.
chaiguy
+7  A: 

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.

Tim C
The problem with this is it's not temporary and won't get cleaned up automatically.
chaiguy
However, the application should create a *subfolder* within that folder and not pollute the base folder with files. Common practice is to use ApplicationData\Manufacturer\Product.
0xA3
@chaiguy: That is true, but in the original question Adam did not ask for a temporary directory that is cleaned up automatically. He asked for a location that "the program can read/write from", "doesn't need them to be anywhere useful to the end-user", and "can be reference programmatically".
Tim C
@chaiguy: There is no folder in Windows that gets cleaned up automatically by default.
0xA3
The system will clean up the Temp directory when you run Hard Disk Cleanup, though it's still better to remove any files you add to it that you don't need anymore.
chaiguy
I edited my original post to clarify the fact that my program only needs access to the files for a few minutes.
Adam S
+6  A: 

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.

TLiebe
Just curious... there's no reason why Windows would decide to delete temp data while the application using it is running, right?
baultista
Windows does not actually clean up the temp directory automatically at all. As a user you must do so manually. Basically it is a nice way of keeping data from applications in a spot that the user knows is only temporary. However, there are utilities which clean out the temp directory for you, and if one is set to go off while your application is executing, it will delete the data while your application is using it. This is relatively rare though.
Tim C
Files in the temp directory should only be deleted manually (by the user) or by the Hard Disk Cleanup process (usually run manually or scheduled). As always, you should wrap your code with some Try..Catch blocks to handle things like File Not Found or there not being enough disk space. You can use the Path.GetTempFileName method to generate a file name as well if you don't really care what the file name is.
TLiebe
I think most cleanup tools will only delete files that haven't been accessed in the last x days. Unless you have a very long running process that only accesses the file every x + n days, you won't have problems with it disappearing prematurely.
Nelson
+1  A: 

I think you want the application data directory:

var appplicationDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
Graham Miller
A: 

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();
    }
}
0xA3