解决Windows Task Scheduler执行exe时 can't open Files

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/coffee_cream/article/details/81054194

问题描述

自己用C#编写了一个exe,希望利用windows task schedule每天定时执行这个exe,其中这个exe读取了同一个文件夹中的config.ini数据,在文件夹中点击exe完全运行正常,但是windows task schedule在run这个exe时总是说“can’t open config.ini”

问题的原因

代码中的config.ini引用的是相对路径,windows task schedule在执行任务时的路径是 c:/system……

解决方法

(1)代码中调用时使用绝对路径;
(2)代码中先读取exe的路径,然后通过这个路径来配置config.ini的绝对路径,例如,c#中可以使用:
string currentPath = System.AppDomain.CurrentDomain.BaseDirectory;

要注意,有些命令在这种情况下获得的是windows task schedule的路径,如:
- System.Environment.CurrentDirectory
- System.IO.Directory.GetCurrentDirectory()

猜你喜欢

转载自blog.csdn.net/coffee_cream/article/details/81054194