无法使用C#代码运行.exe应用程序。

26 浏览
0 Comments

无法使用C#代码运行.exe应用程序。

我有一个exe文件,需要从我的C#程序中调用它,并传入两个参数(PracticeId,ClaimId)。

例如:

假设我有一个名为test.exe的应用程序,其功能是根据给定的两个参数提出索赔。

在命令提示符中,我通常会给出以下命令:

test.exe 1 2

它可以正常工作并完成转换任务。

但是我想使用我的C#代码执行相同的操作。

我使用以下示例代码:

Process compiler = new Process();
compiler.StartInfo.FileName = "test.exe" ;
compiler.StartInfo.Arguments = "1 2" ;
compiler.StartInfo.UseShellExecute = true;
compiler.StartInfo.RedirectStandardOutput = true;
compiler.Start();

当我尝试使用上述代码调用test.exe时,它无法执行创建索赔文本文件的操作。

问题出在哪里?是否涉及多线程或其他问题,我不知道。

请问有人能告诉我需要在上述代码中做出哪些更改吗?

0
0 Comments

无法使用C#代码运行.exe应用程序的问题可能出现的原因是路径错误或者权限不足。解决方法是确保路径正确,同时以管理员权限运行代码。

以下是一个示例代码:

using System;
using System.Diagnostics;
class Program
{
    static void Main()
    {
        try
        {
            string path = @"C:\path\to\xyz.exe";
            ProcessStartInfo processStartInfo = new ProcessStartInfo(path, "1 2");
            processStartInfo.UseShellExecute = true;
            processStartInfo.Verb = "runas"; // 以管理员权限运行
            Process.Start(processStartInfo);
        }
        catch (Exception ex)
        {
            Console.WriteLine("An error occurred: " + ex.Message);
        }
    }
}

上述代码中,`path`变量是指向.exe文件的正确路径。`ProcessStartInfo`类用于指定要启动的进程的信息,包括文件路径和命令行参数。`UseShellExecute`属性设置为`true`以便使用操作系统的Shell来启动进程。`Verb`属性设置为`runas`表示以管理员权限运行。

如果代码仍然无法运行.exe应用程序,可能是由于其他原因导致的问题,例如.exe文件本身损坏或缺少依赖项。在这种情况下,可以尝试重新安装应用程序或者检查是否有其他错误导致无法运行。

0
0 Comments

检查您的工作目录。test.exe在您的路径中吗?如果不在,您将需要提供路径。如果您知道它在哪里,提供路径是一个好的做法。您可以从应用程序路径、执行路径或一些用户首选项设置动态构建它。

Here's an example of how to supply the path using the Application path:

string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "test.exe");
Process.Start(path);

Also, make sure that the test.exe file is not blocked by Windows. Right-click on the file, go to Properties, and if there is an "Unblock" button, click on it.

If the issue still persists, check if the test.exe file is a valid executable file. You can try running it manually to see if it works.

If none of the above solutions work, you can try running the application as administrator. Right-click on your C# project, go to Properties, and in the "Debug" tab, check the "Run this program as an administrator" option.

Hopefully, one of these solutions will help you resolve the issue of being unable to run .exe application using C# code.

0
0 Comments

无法使用C#代码运行.exe应用程序的问题出现的原因是Process对象的UseShellExecute属性必须设置为false才能重定向IO流。

解决方法是使用以下代码,将UseShellExecute属性设置为true,并将RedirectStandardOutput属性设置为false,以禁用重定向:

var compiler = new Process();
compiler.StartInfo.FileName = "test.exe";
compiler.StartInfo.Arguments = "1 2";
compiler.StartInfo.UseShellExecute = true;
compiler.StartInfo.RedirectStandardOutput = false;
compiler.Start();

这样,代码就能正确执行并通过参数传递。

0