How to get the path of app(without app.exe)?

前端 未结 7 1897
梦如初夏
梦如初夏 2020-12-09 06:28

I want to get the path of my app like: \"\\\\ProgramFiles\\\\myApp\", I try to use the following code:


string path = System.Reflection.Assembly.GetExecuting         


        
相关标签:
7条回答
  • 2020-12-09 06:41

    If its an exe as in your case use:

        // Summary:
        //     Gets the path for the executable file that started the 
        //     application, not including the executable name.    
        Application.StartupPath
    
    0 讨论(0)
  • 2020-12-09 06:42
    path = System.IO.Path.GetDirectoryName( path );
    
    0 讨论(0)
  • 2020-12-09 06:47

    More simple than the rest:

    using System.IO;
    using System.Reflection;
    ...
    var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
    
    0 讨论(0)
  • 2020-12-09 06:50

    Application.StartupPath should do that for you.

    Update: from you edit I see that you are running on Compact Framework. Then Application.StartupPath will not work. This is the construct that I usually use then:

    private static string GetApplicationPath()
    {
        return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
    }
    
    0 讨论(0)
  • 2020-12-09 06:56
    Path.GetFileNameWithoutExtension(path);
    
    0 讨论(0)
  • 2020-12-09 07:00

    What about using a FileInfo object to extract the directory name?

    In Vb.Net:

    fi = New FileInfo(System.Reflection.Assembly.GetExecutingAssembly.Location)
    path = fi.DirectoryName
    
    0 讨论(0)
提交回复
热议问题