How can I get the application's path in a .NET console application?

后端 未结 27 1404
甜味超标
甜味超标 2020-11-21 11:11

How do I find the application\'s path in a console application?

In Windows Forms, I can use Application.StartupPath to find the current path, but this d

相关标签:
27条回答
  • 2020-11-21 12:03

    The answer above was 90% of what I needed, but returned a Uri instead of a regular path for me.

    As explained in the MSDN forums post, How to convert URI path to normal filepath?, I used the following:

    // Get normal filepath of this assembly's permanent directory
    var path = new Uri(
        System.IO.Path.GetDirectoryName(
            System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
        ).LocalPath;
    
    0 讨论(0)
  • 2020-11-21 12:03

    You can simply add to your project references System.Windows.Forms and then use the System.Windows.Forms.Application.StartupPath as usual .

    So, not need for more complicated methods or using the reflection.

    0 讨论(0)
  • 2020-11-21 12:04

    Try this simple line of code:

     string exePath = Path.GetDirectoryName( Application.ExecutablePath);
    
    0 讨论(0)
  • 2020-11-21 12:04

    You can create a folder name as Resources within the project using Solution Explorer,then you can paste a file within the Resources.

    private void Form1_Load(object sender, EventArgs e) {
        string appName = Environment.CurrentDirectory;
        int l = appName.Length;
        int h = appName.LastIndexOf("bin");
        string ll = appName.Remove(h);                
        string g = ll + "Resources\\sample.txt";
        System.Diagnostics.Process.Start(g);
    }
    
    0 讨论(0)
  • 2020-11-21 12:05

    you can use this one instead.

    System.Environment.CurrentDirectory
    
    0 讨论(0)
  • 2020-11-21 12:05

    I use this if the exe is supposed to be called by double clicking it

    var thisPath = System.IO.Directory.GetCurrentDirectory();
    
    0 讨论(0)
提交回复
热议问题