Search code examples
c#directorysubdirectoryappdataroaming

How do you locate a folder inside of Roaming using C#?


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.


Solution

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