Search code examples
c#directoryappdata

Creating and using c# Application Folder


I am creating an application that uses photos and an XML file all in one folder that I am creating manually, I want to let the user update the data of that folder (Adding Photos, and Editing the Xml file) at run time via the application my question is what is the best approach and where to put that Folder, I know I have to put that relative paths so I am confiused is it in the AppData if so how to do it.


Solution

  • // Use this to get the common directory
    string CommonDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
    
    // And combine it with your own. 
    // On Window7 this will return "c:\ProgramData\YourCompany\YourProduct"
    string YourDir = Path.Combine(CommonDir, @"YourCompany\YourProduct");
    
    // And create the directory / ensure it exists
    System.IO.Directory.CreateDirectory(YourDir);
    

    There are other Special Folders you can get from the system, such as MyDocuments or Desktop as fits your needs best.