【问题标题】:Scheduler can't find Environment.CurrentDirectory for a running console app [duplicate]调度程序找不到正在运行的控制台应用程序的 Environment.CurrentDirectory [重复]
【发布时间】:2018-03-07 00:05:57
【问题描述】:

我有一个简单的 C# 控制台应用程序,它从位于同一目录中的 config.ini 获取数据。引用config.ini 实现Environment.CurrentDirectory

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

问题是当我使用Scheduler 运行应用程序时,它找不到config.ini 的路径。 Scheduler如何设置这样的路径来运行应用程序?

【问题讨论】:

标签: c# scheduled-tasks


【解决方案1】:

试试这个

// 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);

【讨论】:

  • 除了包含 Windows.Forms 程序集之外,控制台应用程序还有更好的方法来执行此操作...
猜你喜欢
  • 1970-01-01
  • 2012-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-30
  • 2018-11-21
相关资源
最近更新 更多