为什么我的 c Sharp 命令提示符代码在我的服务器上不起作用,但在我的本地电脑上运行良好

发布于 2024-11-26 15:50:57 字数 1165 浏览 0 评论 0原文

我创建了一些代码来执行命令行命令。它在我的开发 PC 上工作正常,但当我尝试在服务器(Windows 2003、IIS 6)上运行代码时,该命令无法运行。

我检查了服务器上的路径,确信它是正确的。我能够从服务器获得一些输出(参见附图)。

服务器输出图像

我认为这可能是权限问题。但我不知道在哪里检查这个问题以及如何纠正它。任何帮助将不胜感激。

更新:

我在本地开发电脑(Win 7 和 Wind XP)上运行此代码,并且运行良好。但是当我在 Windows 2003 服务器上运行该代码时,它就不起作用了。

ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe");
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.UseShellExecute = false;
processStartInfo.CreateNoWindow = true;
Process process = Process.Start(processStartInfo);

if (process != null)
{
process.StandardInput.WriteLine("c:");
process.StandardInput.WriteLine("cd\\");
process.StandardInput.WriteLine("cd C:\\Program Files\\Bank2CSV Pro");

/*This is Dynamic path for our application */

process.StandardInput.WriteLine("bank2csv_pro.exe" + " " + strSavedFilePath + fileName2    + " " + strSavedFilePath + fileNameCsv);

/* This is static path for command prompt*/

process.StandardInput.Close();
string outputString = process.StandardOutput.ReadToEnd(); 

I created some code to execute a command line command. It is working fine on my development PC but when I try to run the code on the server (Windows 2003, IIS 6) the command does not run.

I check the path on the server and I am sure it is correct. I am able to get some output from the server (see the attached image).

server output image

I am thinking this might be a permissions issue. But I am not sure where to check this and how to correct it. Any help will be greatly appreciated.

Update:

I am running this code on my local development PCs (Win 7 and Wind XP) and it is working fine. But as soon as I run the code on my Windows 2003 server it does not work.

ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe");
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.UseShellExecute = false;
processStartInfo.CreateNoWindow = true;
Process process = Process.Start(processStartInfo);

if (process != null)
{
process.StandardInput.WriteLine("c:");
process.StandardInput.WriteLine("cd\\");
process.StandardInput.WriteLine("cd C:\\Program Files\\Bank2CSV Pro");

/*This is Dynamic path for our application */

process.StandardInput.WriteLine("bank2csv_pro.exe" + " " + strSavedFilePath + fileName2    + " " + strSavedFilePath + fileNameCsv);

/* This is static path for command prompt*/

process.StandardInput.Close();
string outputString = process.StandardOutput.ReadToEnd(); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吾家有女初长成 2024-12-03 15:50:57

我经过测试,发现在Web服务器的Web根目录中工作时,能够执行上面代码片段中所示的代码。

所以最后看来是权限问题。为了给出我所做的实际示例,

我将代码从以下

process.StandardInput.WriteLine("cd C:\\Program Files\\Bank2CSV Pro");

代码修改为:

process.StandardInput.WriteLine("cd C:\\Inetpub\\wwwroot\\Bank2CSV Pro");

在此更改之后,我能够执行写入磁盘的命令。显然,问题在于 IUSR 用户必须拥有写入相关目录的权限。我还可以通过授予 ISUR 用户对 Program Files 目录中的文件夹的磁盘写入权限来解决问题,但我认为这不是一个好的做法......

I have been testing and found that I was able to execute the code as shown in the code snippet above when working in the web root directory of the web server.

So in the end it seems it was a permissions problem. To give a practical example of what I did

I modified the code from this:

process.StandardInput.WriteLine("cd C:\\Program Files\\Bank2CSV Pro");

to this code:

process.StandardInput.WriteLine("cd C:\\Inetpub\\wwwroot\\Bank2CSV Pro");

After this change I was able to execute commands that write to disk. Clearly the problem was that the IUSR user has to have permission to write to the directory in question. I could also solve the problem by giving the ISUR user disk write access to the folder in the Program Files directory, but I think this is not good practice...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文