Daniel Lip
Daniel Lip

Reputation: 11319

How can I create a folder in the app folder in the user's AppData folder e.g. C:\User\AppData?

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

Upvotes: 0

Views: 770

Answers (3)

Schecher_1
Schecher_1

Reputation: 554

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);

Upvotes: 1

Vivek Nuna
Vivek Nuna

Reputation: 1

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);

Upvotes: 1

Ermiya Eskandary
Ermiya Eskandary

Reputation: 23602

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.

Upvotes: 1

Related Questions