首页 文章

运行命令提示符命令

提问于
浏览
492

有没有办法从C#应用程序中运行命令提示符命令?如果是这样,我将如何做以下事情:

copy /b Image1.jpg + Archive.rar Image2.jpg

这基本上在JPG图像中嵌入了一个RAR文件 . 我只是想知道是否有办法在C#中自动执行此操作 .

11 回答

  • 10

    您可以在一行中使用CliWrap执行此操作:

    var stdout = new Cli("cmd")
             .Execute("copy /b Image1.jpg + Archive.rar Image2.jpg")
             .StandardOutput;
    
  • 6
    var proc1 = new ProcessStartInfo();
    string anyCommand; 
    proc1.UseShellExecute = true;
    
    proc1.WorkingDirectory = @"C:\Windows\System32";
    
    proc1.FileName = @"C:\Windows\System32\cmd.exe";
    proc1.Verb = "runas";
    proc1.Arguments = "/c "+anyCommand;
    proc1.WindowStyle = ProcessWindowStyle.Hidden;
    Process.Start(proc1);
    
  • 4

    这就是你需要从C#运行shell命令

    string strCmdText;
    strCmdText= "/C copy /b Image1.jpg + Archive.rar Image2.jpg";
    System.Diagnostics.Process.Start("CMD.exe",strCmdText);
    

    EDIT:

    这是为了隐藏cmd窗口 .

    System.Diagnostics.Process process = new System.Diagnostics.Process();
    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
    startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    startInfo.FileName = "cmd.exe";
    startInfo.Arguments = "/C copy /b Image1.jpg + Archive.rar Image2.jpg";
    process.StartInfo = startInfo;
    process.Start();
    

    EDIT: 2

    重要的是,参数以 /C 开头,否则无效 . 如何Scott Ferguson说:它"Carries out the command specified by the string and then terminates."

  • 28

    参考Microsoft.VisualBasic

    Interaction.Shell("copy /b Image1.jpg + Archive.rar Image2.jpg", AppWinStyle.Hide);
    
  • 3

    你可以使用简单的代码编写 .bat 格式的扩展名,批处理文件的代码:

    c:/ copy /b Image1.jpg + Archive.rar Image2.jpg

    使用这个c#代码:

    Process.Start("file_name.bat")

  • 5

    试过@RameshVel解决方案,但我无法在我的控制台应用程序中传递参数 . 如果有人遇到同样的问题,这是一个解决方案:

    using System.Diagnostics;
    
    Process cmd = new Process();
    cmd.StartInfo.FileName = "cmd.exe";
    cmd.StartInfo.RedirectStandardInput = true;
    cmd.StartInfo.RedirectStandardOutput = true;
    cmd.StartInfo.CreateNoWindow = true;
    cmd.StartInfo.UseShellExecute = false;
    cmd.Start();
    
    cmd.StandardInput.WriteLine("echo Oscar");
    cmd.StandardInput.Flush();
    cmd.StandardInput.Close();
    cmd.WaitForExit();
    Console.WriteLine(cmd.StandardOutput.ReadToEnd());
    
  • 78

    您可以使用以下方法实现此目的(如其他答案中所述):

    strCmdText = "'/C some command";
    Process.Start("CMD.exe", strCmdText);
    

    当我尝试上面列出的方法时,我发现我的自定义命令无法使用上面某些答案的语法 .

    我发现需要将更复杂的命令封装在引号中才能工作:

    string strCmdText;
    strCmdText = "'/C cd " + path + " && composer update && composer install -o'";
    Process.Start("CMD.exe", strCmdText);
    
  • 755

    虽然从技术上讲这并没有直接回答提出的问题,但它确实回答了如何做原始海报想要做的事情:组合文件 . 如果有的话,这是一篇帮助新手了解Instance Hunter和Konstantin正在谈论的内容的帖子 .

    这是我用来组合文件的方法(在本例中是jpg和zip) . 请注意,我创建了一个缓冲区,其中填充了zip文件的内容(在小块中而不是在一个大的读取操作中),然后缓冲区被写入jpg文件的后面,直到zip文件的末尾是到达:

    private void CombineFiles(string jpgFileName, string zipFileName)
    {
        using (Stream original = new FileStream(jpgFileName, FileMode.Append))
        {
            using (Stream extra = new FileStream(zipFileName, FileMode.Open, FileAccess.Read))
            {
                var buffer = new byte[32 * 1024];
    
                int blockSize;
                while ((blockSize = extra.Read(buffer, 0, buffer.Length)) > 0)
                {
                    original.Write(buffer, 0, blockSize);
                }
            }
        }
    }
    
  • 1

    是的,有(参见Matt Hamilton评论中的链接),但使用.NET的IO类会更容易也更好 . 您可以使用File.ReadAllBytes来读取文件,然后使用File.WriteAllBytes来编写“嵌入式”版本 .

  • 8

    这里有一些简单的代码版本 . 它也会隐藏控制台窗口 -

    System.Diagnostics.Process process = new System.Diagnostics.Process();
    process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    process.StartInfo.FileName = "cmd.exe";
    process.StartInfo.Arguments = "/C copy /b Image1.jpg + Archive.rar Image2.jpg";
    process.Start();
    
  • 0

    由于某些原因,上述答案都没有帮助,似乎他们在地毯下扫除错误并且难以排除故障 . 所以我最终得到这样的东西,也许它会帮助别人:

    var proc = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = @"C:\Program Files\Microsoft Visual Studio 14.0\Common7\IDE\tf.exe",
            Arguments = "checkout AndroidManifest.xml",
            UseShellExecute = false,
            RedirectStandardOutput = true,
            CreateNoWindow = true,
            WorkingDirectory = @"C:\MyAndroidApp\"
        }
    };
    
    proc.Start();
    

相关问题