从c#运行exe文件时出现问题

发布于 2024-11-24 06:57:34 字数 697 浏览 1 评论 0原文

当我执行 exe 文件 (PVFProject15.exe) 时,它从输入文件 (inputFile.txt) 读取数据并将结果打印到另一个文件 (outputFile.txt) 中。当我双击该exe文件时,它运行良好;它打开控制台窗口,该窗口在创建输出文件之前一直保持打开状态。但是,当我从 c# 运行 (PVFProject15.exe) 时,控制台窗口打开和关闭得非常快,并且永远不会创建输出文件。

我非常感谢您的帮助,因为我一整天都在努力解决这个问题,但从未找到答案。下面是我的代码。

private void button1_Click(object sender, EventArgs e)

{
        Process runFortran = new Process();
        try
        {
            runFortran.StartInfo.FileName = "C:\\temp\\trial\\PVFProject15.exe";
            runFortran.Start();
            runFortran.WaitForExit(); 
        }
        catch (Exception exc)
        {
            MessageBox.Show(exc.Message);
        }
    }

先感谢您。

萨法阿

When I execute an exe file (PVFProject15.exe), it reads the data from an input file (inputFile.txt) and print the results in another file (outputFile.txt). The exe file works well when I double click it; It opens the console window which stays opened until the output file is created. However, when I run (PVFProject15.exe) from c#, the console window opens and closes very quickly and the output file is never created.

I would really appreciate your help since I have been working to fix this for a whole day and never found the answer. Here is my code below.

private void button1_Click(object sender, EventArgs e)

{
        Process runFortran = new Process();
        try
        {
            runFortran.StartInfo.FileName = "C:\\temp\\trial\\PVFProject15.exe";
            runFortran.Start();
            runFortran.WaitForExit(); 
        }
        catch (Exception exc)
        {
            MessageBox.Show(exc.Message);
        }
    }

Thank you in advance.

Safaa

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

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

发布评论

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

评论(3

眼趣 2024-12-01 06:57:34

可能 PVFProject15.exe 需要将当前目录设置为 C:\temp\Trial

Probably PVFProject15.exe needs current directory to be set to C:\temp\trial

旧人 2024-12-01 06:57:34

如果 PVFProject15.exe 使用相对路径写入文件,请在启动主程序引导程序的目录中查找 outputFile.txt

If PVFProject15.exe writes to file using relative path, look for outputFile.txt in directory from which you start your main program-bootstrapper.

我偏爱纯白色 2024-12-01 06:57:34

当我尝试从基于 C# 的软件启动一些 .exe 和 .hta 时,我也遇到了同样的问题。
我开始寻找迈克·莫扎耶夫的解决方案和答案,为我指明了正确的方向。
在您的代码中您需要使用:
StartInfo.WorkingDirectory = Convert.ToString( System.IO.Directory.GetParent(appPath));

所以代码必须是这样的:

 if (File.Exists(appPath))
                {
                    Process runProcess = new Process();
                    runProcess.StartInfo.WorkingDirectory = Convert.ToString( System.IO.Directory.GetParent(appPath));
                    runProcess.StartInfo.UseShellExecute= true;
                    runProcess.StartInfo.FileName = appPath;
                    runProcess.Start();

                }

I also meet with same problem, when I try start some .exe and .hta from my C# based software.
I start to looking for solution and answer of Mike Mozhaev get to me right direction.
In your code you need to use:
StartInfo.WorkingDirectory = Convert.ToString( System.IO.Directory.GetParent(appPath));

So code have to be like this:

 if (File.Exists(appPath))
                {
                    Process runProcess = new Process();
                    runProcess.StartInfo.WorkingDirectory = Convert.ToString( System.IO.Directory.GetParent(appPath));
                    runProcess.StartInfo.UseShellExecute= true;
                    runProcess.StartInfo.FileName = appPath;
                    runProcess.Start();

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