创建/运行命令文件编程 [英] Create/run command file programmatically

查看:148
本文介绍了创建/运行命令文件编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建将安装的.msi,然后执行通过C#代码,CMD文件一个cmd文件。代码工作完美,如果我使用运行F5或控制+ F5从视觉工作室。



不过,有一次我在一个.msi文件打包代码,并安装它(这是一个WPF应用程序),当它执行相关的代码,它会打开命令窗口但它不执行命令。相反,它说:C:\Program不被识别为一个内部或外部命令......我知道这个错误意味着有不在身边文件路径引号但事实并非如此,因为你可以看到下面的报价在那里。另外,如果我去了程序文件目录,Reinstall.cmd文件是否存在,并在有引号。我甚至可以双击生成的Reinstall.cmd文件,它会执行就好了。缺少什么我在这里?



下面是代码:

 私人字符串MSIFilePath = Path.Combine(Environment.CurrentDirectoryHoustersCrawler.msi); 
私人字符串CmdFilePath = Path.Combine(Environment.CurrentDirectoryReinstall.cmd);

私人无效CreateCmdFile()
{
//检查文件是否存在。
如果(File.Exists(CmdFilePath))
File.Delete(CmdFilePath);
//创建新的文件。
VAR网络=新的FileInfo(CmdFilePath);
VAR FILESTREAM = fi.Create();
fileStream.Close();
//写入命令到文件。使用
(TextWriter的作家=新的StreamWriter(CmdFilePath))
{
writer.WriteLine(的String.Format(MSIEXEC / I \{0} \,MSIFilePath)) ; // /安静
}
}

私人无效RunCmdFile()
{//运行命令文件重新安装应用程序。
变种P =新的Process();
p.StartInfo =新的ProcessStartInfo(cmd.exe的,/ K+ CmdFilePath);
p.Start();
p.WaitForExit();
}


解决方案

你可以看到下面的报价都在那里。




您把它们放在第一部分,而不是你在哪里执行的cmd.exe



由于您的路径中有空格,则需要将其包装在引号。尝试:

  p.StartInfo =新的ProcessStartInfo(cmd.exe的,/ K \+ CmdFilePath + \); 


I'm trying to create a cmd file that will install a .msi and then execute that cmd file via C# code. The code works perfectly if I run it using f5 or control + f5 from visual studio.

However, once I package up the code in a .msi file and install it (it's a wpf app), when it executes the relevant code, it opens the command window but it doesn't execute the command. Instead it says: "C:\Program is not recognized as an internal or external command..." I know this error means that there aren't quotes around a file path but that's not the case, as you can see below the quotes are there. Also, if I go to the program files directory, the Reinstall.cmd file is present and has quotes in it. I can even double click the generated Reinstall.cmd file and it will execute just fine. What am I missing here??

Here is the code:

private string MSIFilePath = Path.Combine(Environment.CurrentDirectory, "HoustersCrawler.msi");
        private string CmdFilePath = Path.Combine(Environment.CurrentDirectory, "Reinstall.cmd");

private void CreateCmdFile()
        {
            //check if file exists.
            if (File.Exists(CmdFilePath))
                File.Delete(CmdFilePath);
            //create new file.
            var fi = new FileInfo(CmdFilePath);
            var fileStream = fi.Create();
            fileStream.Close();
            //write commands to file.
            using (TextWriter writer = new StreamWriter(CmdFilePath))
            {
                writer.WriteLine(String.Format("msiexec /i \"{0}\"", MSIFilePath));// /quiet
            }
        }

        private void RunCmdFile()
        {//run command file to reinstall app.
            var p = new Process();
            p.StartInfo = new ProcessStartInfo("cmd.exe", "/k " + CmdFilePath);
            p.Start();
            p.WaitForExit();
        }

解决方案

as you can see below the quotes are there.

You put them in the first portion, but not where you're executing cmd.exe.

Since your path has spaces in it, you need to wrap it in quotes. Try:

p.StartInfo = new ProcessStartInfo("cmd.exe", "/k \"" + CmdFilePath + "\"");

这篇关于创建/运行命令文件编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆