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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8313385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:33:16  来源:igfitidea点击:

How to get application folder when program is started by Windows Task Scheduler

c#.netwindowsscheduled-tasks

提问by Erik Dekker

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?

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

回答by Aaron McIver

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()将返回正在执行的进程的当前目录,在此实例中该目录不是您的应用程序。以上内容足以获取您的可执行文件正在运行的执行目录。

回答by dsolimano

GetCurrentDirectoryreturns 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.

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

回答by abatishchev

Assembly.GetExecutingAssembly().Location

See also GetCallingAssembly()and GetEntryAssembly().

另见GetCallingAssembly()GetEntryAssembly()

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

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

回答by Shyju

Can you try what this returns ?

你能试试这会返回什么吗?

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

回答by Owen

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

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

回答by Ratan

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.

它是一个旧线程,但对于某些人来说,在设置任务时,您可以通过将可选的 :Start in” 值设置为您的 exe 文件夹来分配任务操作中的位置。然后 GetCurrentDirectory 将正常工作。

回答by Alexander

AppDomain.CurrentDomain.BaseDirectoryWill get the directory if you want to use files relative to the install directory.

AppDomain.CurrentDomain.BaseDirectory如果要使用相对于安装目录的文件,将获取该目录。

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