简体   繁体   English

为什么我的控制台应用程序从System32运行?

[英]Why is my Console app running from System32?

I have a console app which is located on my desktop. 我的桌面上有一个控制台应用程序。 I have set it to a Scheduled Task to run every 20 Minutes indefinitely. 我将其设置为“计划任务”以无限期每20分钟运行一次。 I have turned off auto sleep/hibernate. 我已经关闭了自动睡眠/休眠模式。 Then I left my PC ON and locked my desktop for the weekend (2-3 days). 然后我将PC保持开机状态,并在周末(2-3天)锁定了桌面。

My console app was developed to email me every time it catches an exception. 我的控制台应用程序旨在在每次遇到异常时向我发送电子邮件。 When I returned, checked my inbox received a couple of error emails containing 当我返回时,检查我的收件箱收到了几封错误的电子邮件,其中包含

Access to the path 'C:\\WINDOWS\\system32\\myLogs\\' is denied. 拒绝访问路径“ C:\\ WINDOWS \\ system32 \\ myLogs \\”。

it seemed my console app was being run from System32 not from my Desktop . 看来我的控制台应用程序是从System32运行的,而不是从Desktop

Q: Why is it behaving like it? 问:为什么表现如此?


this is my string on creating my myLog folder path 这是我创建myLog文件夹路径时的字符串

var logpath = Directory.GetCurrentDirectory() + Properties.Settings.Default.LogPath;

this checks if the folder exists, if not it creates a new folder. 这将检查文件夹是否存在,如果不存在,则会创建一个新文件夹。

if (!Directory.Exists(logpath))
   Directory.CreateDirectory(logpath);

I believe the error was triggered on checking/creating the folder. 我相信该错误是在检查/创建文件夹时触发的。 My app should create the myLog folder in the same directory as my console app. 我的应用程序应在与控制台应用程序相同的目录中创建myLog文件夹。

Q: Why is it running from System32 Folder? 问:为什么从System32文件夹运行?

Scheduled Tasks are launched by the Task Scheduler service. 计划任务由任务计划程序服务启动。 This service runs inside the C:\\Windows\\System32\\svchost.exe executable. 此服务在C:\\Windows\\System32\\svchost.exe可执行文件中运行。 By default, all applications launched by the Task Scheduler are launched with C:\\Windows\\System32 as the current directory. 默认情况下,任务计划程序启动的所有应用程序都以C:\\Windows\\System32作为当前目录启动。

You can change the start directory in the Edit Action dialog of the Task Scheduler: 您可以在任务计划程序的“编辑操作”对话框中更改开始目录:

任务计划程序:“编辑操作”对话框

You can use environment variables. 您可以使用环境变量。 For example, %USERPROFILE% will set the start directory to the user's profile directory (eg. C:\\Users\\MyUsername). 例如, %USERPROFILE%将开始目录设置为用户的配置文件目录(例如C:\\ Users \\ MyUsername)。

Rather than changing the start directory for the scheduled task, you may want to find the the directory where the console application executable is located : 您可能要查找控制台应用程序可执行文件所在的目录,而不是更改计划任务的开始目录:

System.Reflection.Assembly.GetExecutingAssembly().Location

Console applications are "command line" and they are run from CMD.EXE which is usually located in C:\\Windows\\System32\\ 控制台应用程序是“命令行”,它们是从CMD.EXE运行的,该文件通常位于C:\\ Windows \\ System32 \\

I suspect the scheduled task sets the current directory to where CMD.EXE is located and then launches your application by specifying the full folder, 我怀疑计划任务将当前目录设置为CMD.EXE所在的位置,然后通过指定完整文件夹来启动您的应用程序,

(note there is one in \\Syswow64\\ for 32-bit command lines for 64-bit machines). (请注意,对于64位计算机,32位命令行在\\ Syswow64 \\中存在)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM