简体   繁体   English

如何在Windows任务计划程序启动程序时获取应用程序文件夹

[英]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如果要使用相对于安装目录的文件,将获取该目录。

Also see Best way to get application folder path 另请参阅获取应用程序文件夹路径的最佳方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Windows Task Scheduler终止程序在窗体应用程序上不起作用 - Windows task scheduler terminate program doesn't work on forms application 当我的应用程序失败时如何通知 Windows 任务计划程序? - How do I notify Windows Task Scheduler when my application fails? 如何使用Windows Task Scheduler隐藏控制台应用程序窗口 - How to hide console application window with windows task scheduler 是否可以在自己的应用程序中使用Windows 7任务计划程序 - Is it possible to use Windows 7 Task scheduler in own application 从任务计划程序调试Windows控制台应用程序 - Debugging a Windows Console Application from Task Scheduler 任务计划程序启动时隐藏C#控制台应用程序窗口 - Hide C# Console App Window When Started by Task Scheduler 如何识别进程是手动启动还是从任务计划程序启动? - How to identify the process is started manually or from Task Scheduler? Windows Server Task Scheduler需要文件在System32文件夹中吗? - Windows Server Task Scheduler needs files to be in System32 folder? 如何从服务器上的Windows Task Scheduler运行ac#控制台应用程序 - How to run a c# console application from windows task scheduler on server C#为什么我不能以编程方式杀死在Task Scheduler中启动的控制台程序? - C# Why can I not programmatically kill a console program started in Task Scheduler?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM