Instead of using the environment directly I suggest that you use some of the support the .NET API can provide. There are two functions for temporary files:
Path.GetTempPath()
returns the path of the current user's temporary folder.Path.GetTempFileName()
creates an empty file with a unique name in the user's temporary folder.
The temporary folder is actually found using the environment by using the Windows GetTempPath function. Typically the folder will be C:\Users\UserName\AppData\Local\Temp
.
If you want an application specific folder for you own use you can employ a special folder:
const String CompanyName = "Acme Industries";
const String ApplicationName = "FooBar";
var subfolderName = Path.Combine(CompanyName, ApplicationName);
var folderName = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
subfolderName
);
Directory.CreateDirectory(folderName);
Typically the folder created will have the name C:\Users\UserName\AppData\Acme Industries\FooBar
. Your application is free to use this folder and will not run into "read-only" issues.
Relying on a standard API allows your application to reliably run on different versions of Windows and in different environments (e.g. a terminal server).