5

I'm trying to figure out a way to navigate to a sub folder in Roaming using C#. I know to access the folder I can use:

string folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

What I'm trying to do is navigate to a folder inside of Roaming, but do not know how. I basically need to do something like this:

string insideroaming = string folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData\FolderName);

Any way to do this? Thanks.

2 Answers2

8

Consider Path.Combine:

string dir = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
    "FolderName"
);

It returns something similar to:

C:\Users\<UserName>\AppData\Roaming\FolderName

If you need to get a file path inside the folder, you may try

string filePath = Path.Combine(
    dir,
    "File.txt"
);

or just

string filePath = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
    "FolderName",
    "File.txt"
);
AlexD
  • 32,156
  • 3
  • 71
  • 65
  • Let me make the question more clear, because I could see how someone could look at my question differently the way I wrote it. I have already combined the paths to create the folder, but what I need to do next is add a file into it. Would I use dir (Which refers to the string that I used for Path.Combine) to access this folder? –  Sep 07 '14 at 01:26
  • @Xceptionist You can use the same method or its overload to build the file path. – AlexD Sep 07 '14 at 01:34
  • @Xceptionist If this answered your question, please mark it as the answer so it can help people who have a similar question in the future. – Patrick Quirk Sep 07 '14 at 02:04
-1
string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string filePath = Path.Combine(appDataFolder + "\\SubfolderName\\" + "filename.txt");
Pang
  • 9,564
  • 146
  • 81
  • 122
Raj
  • 29
  • 2
  • It seems counterproductive (not to mention messy) to use a combination of `Path.Combine` and string concatenation - why not just use `Path.Combine` for the whole thing? – Simon MᶜKenzie Oct 19 '16 at 05:08