[英]How to get application folder when program is started by Windows Task Scheduler
I have a console app in c# thats starts on schuled times by the Windows task scheduler. 我在c#中有一个控制台应用程序,它由Windows任务调度程序在学习时间开始。 The app needs some physical files from its own directory and uses
System.IO.Directory.GetCurrentDirectory()
for that. 该应用程序需要来自其自己目录的一些物理文件,并使用
System.IO.Directory.GetCurrentDirectory()
。
Normal when I start the console app myself, it works perfectly. 正常情况下,当我自己启动控制台应用程序时,它运行正常 But when it is started by Windows Task Scheduler, it returns
C:\\Windows\\System32
. 但是当它由Windows任务计划程序启动时,它将返回
C:\\Windows\\System32
。
Why is this not the application directory and is there another way how I can get the application directory? 为什么这不是应用程序目录,还有另外一种方法可以获取应用程序目录吗?
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
System.IO.Directory.GetCurrentDirectory()
will return the current directory of the executing process which is not your application in this instance. System.IO.Directory.GetCurrentDirectory()
将返回执行进程的当前目录,该目录不是此实例中的应用程序。 The above will suffice in getting the execution directory your executable is running in. 以上内容足以获取运行可执行文件的执行目录。
GetCurrentDirectory
returns that directory because when the scheduler starts an application by default. GetCurrentDirectory
返回该目录,因为默认情况下调度程序启动应用程序时。 If you want to know the directory that your binary is in, you can use 如果您想知道二进制文件所在的目录,可以使用
Assembly.GetExecutingAssembly().Location
I would also be curious to know if you have a "Start In" directory set in your scheduled task - setting that should also set the current directory of the application when it starts. 我也很想知道您是否在计划任务中设置了“开始”目录 - 该设置还应该在应用程序启动时设置当前目录。
Assembly.GetExecutingAssembly().Location
See also GetCallingAssembly()
and GetEntryAssembly()
. 另请参见
GetCallingAssembly()
和GetEntryAssembly()
。
And What is the best way to determine application root directory? 确定应用程序根目录的最佳方法是什么?
你能尝试一下这回归吗?
System.IO.Path.GetDirectoryName(Application.ExecutablePath)
我使用My.Application.Info.DirectoryPath
指向Windows任务调度程序中所需的正确目录。
它是一个旧线程,但对于有人看,在设置任务时,您可以通过设置可选项来分配任务操作中的位置:启动“值到您的exe文件夹.GetCurrentDirectory将正常工作。
AppDomain.CurrentDomain.BaseDirectory
Will get the directory if you want to use files relative to the install directory. AppDomain.CurrentDomain.BaseDirectory
如果要使用相对于安装目录的文件,将获取该目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.