使用process.start()运行cmd.exe时如何引用存储在visual studio项目资源中的exe文件

问题描述 投票:0回答:1

我正在开发一个C#windows窗体应用程序项目。表单包含一个按钮,在按钮单击事件后,使用Process.Start()方法启动cmd.exe。 StartInfo.Arguments()包含存储在项目资源文件夹中的exe文件的路径(我创建了一个名为Resources的新文件夹和另一个名为SW1的子文件夹.exe文件位于SW1文件夹中)

enter image description here

现在,exe文件存储在本地计算机的不同位置,我使用完整路径在StartInfo.Argument()中引用exe文件。但是,当我发布C#应用程序时,我希望exe文件将作为资源捆绑。在这种情况下如何引用exe文件?

private async void button1_Click(object sender, EventArgs e)
    {
        await RunCMDAsync();
    }

private async static Task RunCMDAsync()
    {


        try
        {
            using (Process myProcess = new Process())
            {
                ProcessStartInfo startInfo = new ProcessStartInfo();

                startInfo.FileName = "cmd.exe";

                startInfo.Arguments = @"/C cd <path to the exe file on my local computer here> & <name of exe --additional arguments>";

                startInfo.UseShellExecute = false;
                startInfo.RedirectStandardInput = true;                    
                myProcess.StartInfo = startInfo;
                myProcess.Start();                    
                myProcess.WaitForExit();
            }
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }


    }

在上面的代码中,每当启动cmd.exe进程时,我想使用StartInfo.Arguments()传递两个参数

startInfo.Arguments = @"/C cd <path to the exe file on my local computer here> & <name of exe --additional arguments>";

第一个参数是cd进入包含exe文件的文件夹(/C cd <path to the exe file on my local computer here>

第二个参数运行带有一些参数的exe文件(<name of exe --additional arguments>

在将其作为独立应用程序发布后,如何在Resources> SW1文件夹中引用exe文件?在安装C#应用程序期间是否需要将exe文件解压缩到目标计算机上的某个位置并使用与上面相同的代码,或者有没有办法直接引用资源文件夹?

c# process exe system.diagnostics
1个回答
0
投票

如果你需要的是找到你的程序运行的目录(我不确定我理解你的问题)然后使用System.AppDomain.CurrentDomain.BaseDirectory

string exePath = System.AppDomain.CurrentDomain.BaseDirectory +
 "Resources\\SW1\\" + exefilenameWithExtension;
© www.soinside.com 2019 - 2024. All rights reserved.