【问题标题】:Console App launched from windows service has no file system access从 Windows 服务启动的控制台应用程序没有文件系统访问权限
【发布时间】:2012-05-26 06:38:48
【问题描述】:

我有一个旨在启动多个控制台应用程序的 Windows 服务。控制台应用程序将写入物理日志文件以及文件系统操作。

当服务启动控制台时,它们无法访问文件系统,日志文件中也不会出现任何内容。

当我通过双击可执行文件手动启动控制台时,它们写入日志文件没有问题。

我尝试在本地系统、本地服务、网络服务、本地管理员帐户甚至我自己的登录凭据下运行该服务:

这是启动进程的代码:

Process p = new Process();
p.StartInfo.FileName = agent.AgentLocation; /// Physical path to executable
p.StartInfo.CreateNoWindow = true;
p.StartInfo.ErrorDialog = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p.Start();

【问题讨论】:

  • 不指定 ProcessStartInfo.WorkingDirectory 始终是丢失文件的好方法。查看 c:\windows\system32
  • 如果您正在使用现代 Windows 机器,也不要忘记 UAC。但我认为汉斯做到了。
  • 您确定控制台应用程序已启动吗?设置CreateNoWindow=false 以查看控制台中发生了什么。
  • 我会尝试设置工作目录,但控制台会写入在别处定义的完全独立的文件夹。感谢您的建议
  • 控制台应用程序已启动,我可以在任务管理器中看到它们。我正在实现对数据库的日志记录。

标签: c# .net windows file service


【解决方案1】:

更新:

问题原来是需要如下设置工作目录:

Process p = new Process(); 
p.StartInfo.FileName = agent.AgentLocation; /// Physical path to executable 
p.StartInfo.WorkingDirectory = agent.AgentLocation.Substring(0, agent.AgentLocation.LastIndexOf("\\") + 1);
p.StartInfo.CreateNoWindow = true; 
p.StartInfo.ErrorDialog = false; 
p.StartInfo.RedirectStandardError = true; 
p.StartInfo.RedirectStandardInput = true; 
p.StartInfo.RedirectStandardOutput = true; 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
p.Start(); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-12
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多