Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# create dir in %AppData%

Tags:

c#

directory

I want create directory (if not exist) in path AppData/Roaming/test. But my code doesn't work, I dont know why. Can you help me?

        string path;
        path = @"%AppData%\Roaming\test\";
        path = Environment.ExpandEnvironmentVariables(path);
        Console.WriteLine(path);

        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);

This code doen't create dir.

like image 703
wrepaest Avatar asked May 03 '15 11:05

wrepaest


1 Answers

%AppData% is a SpecialFolder.

change your code from:

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

to:

var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var path = Path.Combine(appDataPath, @"test\");
if (!Directory.Exists(path))
        Directory.CreateDirectory(path);
like image 93
Old Fox Avatar answered Sep 28 '22 03:09

Old Fox