Home > Back-end >  How can I create a folder in the app folder in the user's AppData folder e.g. C:\User\AppData
How can I create a folder in the app folder in the user's AppData folder e.g. C:\User\AppData

Time:10-17

I've tried this:

var systemPath = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
var complete = Path.Combine(systemPath, extractfilename);

But it results in:

C:\ProgramData\Extract.txt

My expected output is:

C:\User\AppData\Extract.txt

CodePudding user response:

C:\User\AppData\Local

Use Environment.SpecialFolder.LocalApplicationData:

The directory that serves as a common repository for application-specific data that is used by the current, non-roaming user.


C:\User\AppData\Roaming

Use Environment.SpecialFolder.ApplicationData:

The directory that serves as a common repository for application-specific data for the current roaming user. A roaming user works on more than one computer on a network. A roaming user's profile is kept on a server on the network and is loaded onto a system when the user logs on.

CodePudding user response:

You need to create file in Environment.SpecialFolder.ApplicationData folder. There is another way also to get its value, so you can use it also and append the path.

e.g.

string path;
path = @"%AppData%\test";

Environment.ExpandEnvironmentVariables(path);

CodePudding user response:

This will create a folder named "MyName" in "%appdata%".

    string directoryName = "MyName";
    string appDataPath =  Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
    string mainPath = Path.Combine(appDataPath, directoryName);
    Directory.CreateDirectory(mainPath);

This will create a file in "%appdata%" called "MyFile.txt" which says "Hello World".

    string text = "Hello Word";
    string fileName = "MyFile.txt";
    string appDataPath =  Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
    string mainPath = Path.Combine(appDataPath, fileName);
    File.WriteAllText(mainPath, text);
  •  Tags:  
  • c#
  • Related