调度程序找不到正在运行的控制台应用程序的Environment.CurrentDirectory

时间:2018-03-07 00:05:58

标签: c# scheduled-tasks

我有简单的C#控制台应用程序,可以从同一目录中的config.ini获取数据。参考config.ini实施的Environment.CurrentDirectory

IniFile myIni = new IniFile(Environment.CurrentDirectory + "\\config.ini");

问题是,当我使用Scheduler运行应用时,它无法找到config.ini的路径。如何设置此类路径以使用Scheduler运行应用程序?

1 个答案:

答案 0 :(得分:0)

试试这个

// to get the location the assembly is executing from
//(not necessarily where the it normally resides on disk)
// in the case of the using shadow copies, for instance in NUnit tests, 
// this will be in a temp directory.
string path = System.Reflection.Assembly.GetExecutingAssembly().Location;

//To get the location the assembly normally resides on disk or the install directory
string path = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;

//once you have the path you get the directory with:
var directory = System.IO.Path.GetDirectoryName(path);