繁体   English   中英

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

[英]How to reference an exe file stored in visual studio project resource when running cmd.exe using process.start()

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

在此输入图像描述

现在,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>

第二个参数使用一些参数( <name of exe --additional arguments> )运行exe文件

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

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

string exePath = System.AppDomain.CurrentDomain.BaseDirectory +
 "Resources\\SW1\\" + exefilenameWithExtension;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM