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

时间:2022-12-22 02:30:08

I have a console app in c# thats starts on schuled times by the Windows task scheduler. The app needs some physical files from its own directory and uses System.IO.Directory.GetCurrentDirectory() for that.

我在c#中有一个控制台应用程序,它由Windows任务调度程序在学习时间开始。该应用程序需要来自其自己目录的一些物理文件,并使用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?

为什么这不是应用程序目录,还有另一种方法可以获取应用程序目录吗?

7 个解决方案

#1


24  

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. The above will suffice in getting the execution directory your executable is running in.

System.IO.Directory.GetCurrentDirectory()将返回执行进程的当前目录,该目录不是此实例中的应用程序。以上内容足以获取运行可执行文件的执行目录。

#2


4  

GetCurrentDirectory returns that directory because when the scheduler starts an application by default. If you want to know the directory that your binary is in, you can use

GetCurrentDirectory返回该目录,因为默认情况下调度程序启动应用程序时。如果您想知道二进制文件所在的目录,可以使用

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.

我也很想知道您是否在计划任务中设置了“启动”目录 - 该设置还应该在启动时设置应用程序的当前目录。

#3


3  

Assembly.GetExecutingAssembly().Location

See also GetCallingAssembly() and GetEntryAssembly().

另请参见GetCallingAssembly()和GetEntryAssembly()。

And What is the best way to determine application root directory?

确定应用程序根目录的最佳方法是什么?

#4


1  

Can you try what this returns ?

你能尝试一下这回归吗?

System.IO.Path.GetDirectoryName(Application.ExecutablePath) 

#5


0  

I use My.Application.Info.DirectoryPath is point to the correct directory what you want within Windows task scheduler.

我使用My.Application.Info.DirectoryPath指向Windows任务调度程序中所需的正确目录。

#6


0  

Its an old thread, but for someone looking, while setting up the task, you can assign the location in the Action of the task, by setting the optional :Start in" value to your exe folder. GetCurrentDirectory will work fine then.

它是一个旧线程,但对于有人看,在设置任务时,您可以通过设置可选项来分配任务操作中的位置:启动“值到您的exe文件夹.GetCurrentDirectory将正常工作。

#7


0  

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

#1


24  

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. The above will suffice in getting the execution directory your executable is running in.

System.IO.Directory.GetCurrentDirectory()将返回执行进程的当前目录,该目录不是此实例中的应用程序。以上内容足以获取运行可执行文件的执行目录。

#2


4  

GetCurrentDirectory returns that directory because when the scheduler starts an application by default. If you want to know the directory that your binary is in, you can use

GetCurrentDirectory返回该目录,因为默认情况下调度程序启动应用程序时。如果您想知道二进制文件所在的目录,可以使用

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.

我也很想知道您是否在计划任务中设置了“启动”目录 - 该设置还应该在启动时设置应用程序的当前目录。

#3


3  

Assembly.GetExecutingAssembly().Location

See also GetCallingAssembly() and GetEntryAssembly().

另请参见GetCallingAssembly()和GetEntryAssembly()。

And What is the best way to determine application root directory?

确定应用程序根目录的最佳方法是什么?

#4


1  

Can you try what this returns ?

你能尝试一下这回归吗?

System.IO.Path.GetDirectoryName(Application.ExecutablePath) 

#5


0  

I use My.Application.Info.DirectoryPath is point to the correct directory what you want within Windows task scheduler.

我使用My.Application.Info.DirectoryPath指向Windows任务调度程序中所需的正确目录。

#6


0  

Its an old thread, but for someone looking, while setting up the task, you can assign the location in the Action of the task, by setting the optional :Start in" value to your exe folder. GetCurrentDirectory will work fine then.

它是一个旧线程,但对于有人看,在设置任务时,您可以通过设置可选项来分配任务操作中的位置:启动“值到您的exe文件夹.GetCurrentDirectory将正常工作。

#7


0  

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