0

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

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • 1
    Change `Environment.SpecialFolder.CommonApplicationData` to `Environment.SpecialFolder.ApplicationData` - does that give you what you need? – Ermiya Eskandary Oct 16 '21 at 20:52

3 Answers3

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);
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
1

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.

Ermiya Eskandary
  • 15,323
  • 3
  • 31
  • 44
1

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);
Schecher_1
  • 343
  • 4
  • 12
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 16 '21 at 22:55