C#: Get common paths

Get application path

/// <summary>
/// Returns the application path
/// </summary>
/// <returns></returns>
public static string GetApplicationPath()
{
    String s = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
    s = System.IO.Path.GetDirectoryName(s);
    s = new Uri(s).LocalPath;

    return s;
}

Short version

Single line version:

string path = new Uri(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)).LocalPath;

Short version with filename from application folder

Get the path from the application folder with additional filename:

string path = new Uri(Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase), "log4net.config")).LocalPath;

Get temp folder

String tempPath = Path.GetTempPath();

Get appdata (application data) path

String appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); 


Datenschutzerklärung | Impressum