大家好,我的exe 放在C:\Test下面,然后有个程序在C:\Soft里面,这个程序里面有个按钮是启动我的exe,我怎么才能获得我exe所在的目录呢?目前我采用的方法:
System.Environment.CurrentDirectory
System.Windows.Forms.Application.StartupPath获取出来的都是C:\Soft,并不是我exe真正所在的目录?求指导

解决方案 »

  1.   

    try thisConsole.WriteLine(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
      

  2.   

    orAppDomain.CurrentDomain.SetupInformation.ApplicationBase
      

  3.   

    apppath = @"c:\test";
    exepath = apppath + "\..\soft";
      

  4.   

    目前我采用的方法:
    System.Environment.CurrentDirectory
    System.Windows.Forms.Application.StartupPath获取出来的都是C:\Soft,并不是我exe真正所在的目录?
    ---------------------------
    这两个是获取当前程序的所在路径,对了没有支持的EXE程序,他是不知道的,
    建议你用配置文件指定EXE的路径
      

  5.   


    string str="";
    str += "\r\n" + System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
    str += "\r\n" + System.Environment.CurrentDirectory;
    str += "\r\n" + System.IO.Directory.GetCurrentDirectory();
    str += "\r\n" + System.AppDomain.CurrentDomain.BaseDirectory;
    str += "\r\n" + System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
    str += "\r\n" + System.Windows.Forms.Application.StartupPath;
    str += "\r\n" + System.Windows.Forms.Application.ExecutablePath;
    Console.Write(str);
    Console.ReadKey();
    得到结果:
    D:\1\bin\Debug\路径测试.vshost.exe
    D:\1\bin\Debug
    D:\1\bin\Debug
    D:\1\bin\Debug\
    D:\1\bin\Debug\
    D:\1\bin\Debug
    D:\1\bin\Debug\路径测试.EXE
    1.   System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
         获取模块的完整路径。
    2.   System.Environment.CurrentDirectory
         获取和设置当前目录(该进程从中启动的目录)的完全限定目录。
    3.   System.IO.Directory.GetCurrentDirectory() 
         获取应用程序的当前工作目录。这个不一定是程序从中启动的目录啊,有可能程序放在C:\www里,这个函数有可能返回C:\Documents and Settings\ZYB\,或者C:\Program Files\Adobe\,有时不一定返回什么东东,我也搞不懂了。
    4. System.AppDomain.CurrentDomain.BaseDirectory
         获取程序的基目录。
    5. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
         获取和设置包括该应用程序的目录的名称。
    6. System.Windows.Forms.Application.StartupPath 
         获取启动了应用程序的可执行文件的路径。效果和2、5一样。只是5返回的字符串后面多了一个"\"而已
    7. System.Windows.Forms.Application.ExecutablePath
         获取启动了应用程序的可执行文件的路径及文件名,效果有时和1一样。
    不解释   你懂的
      

  6.   

    你的那两行代码应该是获取当前exe应用程序的路径吧~路径写死直接去调用不可以吗?