//1.获取模块的完整路径。
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
//E:\工作目录\NewTFS\H2控制台应用程序\KILL\bin\Debug\KILL.vshost.exe
//2.获取和设置当前目录(该进程从中启动的目录)的完全限定目录
System.Environment.CurrentDirectory;
//E:\工作目录\NewTFS\H2控制台应用程序\KILL\bin\Debug
//3.获取应用程序的当前工作目录
System.IO.Directory.GetCurrentDirectory();
//E:\工作目录\NewTFS\H2控制台应用程序\KILL\bin\Debug
//4.获取程序的基目录
System.AppDomain.CurrentDomain.BaseDirectory;
//E:\工作目录\NewTFS\H2控制台应用程序\KILL\bin\Debug\
//5.获取和设置包括该应用程序的目录的名称
System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//E:\工作目录\NewTFS\H2控制台应用程序\KILL\bin\Debug\
//6.获取启动了应用程序的可执行文件的路径
System.Windows.Forms.Application.StartupPath;
//E:\工作目录\NewTFS\H2控制台应用程序\KILL\bin\Debug
//7.获取启动了应用程序的可执行文件的路径及文件名
System.Windows.Forms.Application.ExecutablePath;
//E:\工作目录\NewTFS\H2控制台应用程序\KILL\bin\Debug\KILL.EXE
这些获取目录的,网上一查一大把,简单清晰明了。
自己写的一个简单的任务计划的服务,就是读取一个配置里面的信息,定时去调用对应路径的控制台应用程序。
然后我个人比较喜欢用System.AppDomain.CurrentDomain.BaseDirectory;
SO没遇到什么诡异的问题,自己也用了一年多了也没觉得有什么问题。
今天遇到的是,同事跑来跟我说没有执行他的exe,我去查了日志,的确是调用过了,为什么没执行?然后我就大致看看了他的代码也没发现什么问题,单独执行也正常。
让同事找日志发过来
- <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
- <System>
<Provider Name=".NET Runtime" />
<EventID Qualifiers="0">1026</EventID>
<Level>2</Level>
<Task>0</Task>
<Keywords>0x80000000000000</Keywords>
<TimeCreated SystemTime="2019-07-22T03:13:15.112571700Z" />
<EventRecordID>13692</EventRecordID>
<Channel>Application</Channel>
<Computer>iZ3mxld2ji6o3bZ</Computer>
<Security />
</System>
- <EventData>
<Data>应用程序: XXXXX.exe Framework 版本: v4.0.30319 说明: 由于未经处理的异常,进程终止。异常信息: System.UnauthorizedAccessException 在 System.IO.__Error.WinIOError(Int32, System.String) 在 System.IO.FileStream.Init(System.String, System.IO.FileMode, System.IO.FileAccess, Int32, Boolean, System.IO.FileShare, Int32, System.IO.FileOptions, SECURITY_ATTRIBUTES, System.String, Boolean, Boolean, Boolean) 在 System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, Int32, System.IO.FileOptions, System.String, Boolean, Boolean, Boolean) 在 System.IO.StreamWriter.CreateFile(System.String, Boolean, Boolean) 在 System.IO.StreamWriter..ctor(System.String, Boolean, System.Text.Encoding, Int32, Boolean) 在 System.IO.StreamWriter..ctor(System.String, Boolean) 在 XXXXX.Program.Write(System.DateTime, System.String, System.String) 在 XXXXX.Program.Main(System.String[])</Data>
</EventData>
</Event>
I/O 写文件?难道是目录错了?
结果他的路径使用的是System.Environment.CurrentDirectory;去获取的
最后我得到的结果是我服务下面的目录,然后根本找不到那个exe,坑坑坑!
做了下测试:
创建了2个控制台应用程序,分别输出上面那一堆目录,第一个去调用第二个。
第一个exe输出的日志:
E:\工作目录\NewTFS\H2控制台应用程序\KILL\bin\Debug\KILL.exe
E:\工作目录\NewTFS\H2控制台应用程序\KILL\bin\Debug
E:\工作目录\NewTFS\H2控制台应用程序\KILL\bin\Debug
E:\工作目录\NewTFS\H2控制台应用程序\KILL\bin\Debug\
E:\工作目录\NewTFS\H2控制台应用程序\KILL\bin\Debug\
E:\工作目录\NewTFS\H2控制台应用程序\KILL\bin\Debug
E:\工作目录\NewTFS\H2控制台应用程序\KILL\bin\Debug\KILL.exe
调用第二个exe输出的日志:
E:\工作目录\NewTFS\H2控制台应用程序\KILL2\bin\Debug\KILL2.exe
E:\工作目录\NewTFS\H2控制台应用程序\KILL\bin\Debug
E:\工作目录\NewTFS\H2控制台应用程序\KILL\bin\Debug
E:\工作目录\NewTFS\H2控制台应用程序\KILL2\bin\Debug\
E:\工作目录\NewTFS\H2控制台应用程序\KILL2\bin\Debug\
E:\工作目录\NewTFS\H2控制台应用程序\KILL2\bin\Debug
E:\工作目录\NewTFS\H2控制台应用程序\KILL2\bin\Debug\KILL2.exe
System.Environment.CurrentDirectory;
System.IO.Directory.GetCurrentDirectory();
对,就是这2个,看吧,输出的第一个exe的目录,找到坑了,然后把System.Environment.CurrentDirectory改成我常用的System.AppDomain.CurrentDomain.BaseDirectory,解决了。