【问题标题】:cmd.exe and adb commandcmd.exe 和 adb 命令
【发布时间】:2021-12-20 21:52:03
【问题描述】:

您好,我在通过 c# 使用 cmd 命令时遇到了一些问题 例如我在 cmd.exe 中手动执行命令

//command1 to pick directory
cd C:\Users\NewSystem\source\repos
//command2 command to send file to emulator
adb -s emulator-5554 push Debug\funnels\01\01.mp4 /sdcard/Download

我在 C# 中测试的代码是,它不能正常工作,完全没有向 android 模拟器发送任何内容

Process cmd = new Process();
            cmd.StartInfo.FileName = "cmd.exe";
            cmd.StartInfo.WorkingDirectory = @"C:\Users\NewSystem\source\repos\";
            cmd.StartInfo.RedirectStandardInput = true;
            cmd.StartInfo.RedirectStandardOutput = false;
            cmd.StartInfo.CreateNoWindow = false;
            cmd.StartInfo.UseShellExecute = false;
            cmd.Start();

            cmd.StandardInput.WriteLine("adb -s emulator-5554 push Debug\funnels\01\01.mp4 /sdcard/Download");
            cmd.StandardInput.Flush();
            cmd.StandardInput.Close();
            cmd.WaitForExit();

【问题讨论】:

    标签: c# .net process adb command-line-arguments


    【解决方案1】:

    要启动cmd.exe 并让它执行您需要传递的命令/c(执行命令并退出)或/k(执行命令并保持)...

    cmd /c adb -s emulator-5554 push Debug\funnels\01\01.mp4 /sdcard/Download
    

    不过,您不需要 cmd.exe 启动 adb.exe;直接启动adb.exe即可。

    至于您的 C# 代码,StandardInput 用于写入进程的stdin stream,这不是您想要的。传递命令行参数的方法是使用ProcessStartInfoArgumentList(.NET (Core) 2.1+)或Arguments(所有.NET Framework/Core 版本)属性...

    // Dispose cmd when finished
    using (Process cmd = new Process())
    {
        // The full executable path is required if its directory is not in %PATH%
        cmd.StartInfo.FileName = @"C:\Users\NewSystem\source\repos\adb.exe";
        cmd.StartInfo.Arguments = @"-s emulator-5554 push ""Debug\funnels\01\01.mp4"" ""/sdcard/Download""";
        cmd.StartInfo.WorkingDirectory = @"C:\Users\NewSystem\source\repos\";
        cmd.StartInfo.CreateNoWindow = false;
        cmd.StartInfo.UseShellExecute = false;
    
        cmd.Start();
        cmd.WaitForExit();
    }
    

    我将 Arguments 设置为 verbatim string 并引用最后两个参数,只是为了说明如果任一路径包含空格,您将如何做到这一点。

    【讨论】:

      猜你喜欢
      • 2012-10-13
      • 2016-08-01
      • 1970-01-01
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-22
      相关资源
      最近更新 更多