无法使用 c# 运行 azcopy 工具

Unable to run azcopy tool with c#

我已经编写了一个控制台应用程序来以编程方式调用 AZcopy 工具,但我无法调用 azcopy 工具。你能给我重定向可行的来源吗?我已经下载 azcopy_windows_amd64_10.12.1 并将 azcopy.exe 放入 C:\Windows\System32\azcopy.exe location

static void Main(string[] args)
    {
        string strCmdText = @"AzCopy.exe sync ""D:\temp"" ""https://myhubforazcopy.blob.core.windows.net/myhubforazcopy1?sp=racwdl&st=2021-09-05T17:19:11Z&se=2021-09-06T01:19:11Z&spr=https&sv=2020-08-04&sr=c&sig=MAraJ0PxqJMDdYuWzrOUEwYda%2BkXEukP%2Fs%3D"" --destination-delete=true";
       
        CallProcess(strCmdText);
    }

public static void CallProcess(string strCmdText)
    {
       
        //C:\Windows\System32\azcopy.exe
        var process = new Process();
        ProcessStartInfo startInfo = new ProcessStartInfo
        {
            FileName = @"C:\Windows\System32\cmd.exe",
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            Arguments = strCmdText,
            UseShellExecute = false,
            CreateNoWindow = false,
            WorkingDirectory = @"D:\AzCopy\bin\Debug\"
        };


        process.StartInfo = startInfo;

        process.Start();

        process.StandardInput.Flush();
        process.StandardInput.Close();
        process.WaitForExit();
        Console.WriteLine(process.StandardOutput.ReadToEnd());
    }

从您要执行的程序开始 cmd,因为参数实际上 运行t 该程序,您需要在 [= 之后传递整个参数(您的程序) =13=] 标志,像这样:

string strCmdText = @"/c AzCopy.exe sync ""D:\temp"" ...

确保 AzCopy 在您的 PATH 中或指定它的精确路径

但是如果您直接 运行 AzCopy 而不是启动命令提示符然后启动 AzCopy 会更好,这样做会像这样:

// Notice how the 'azcopy' at the start is gone
var arguments = @"""D:\temp"" ""https://myhubforazcopy.blob ..."

var process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo
{
    FileName = @"C:\Windows\System32\azcopy.exe",
    RedirectStandardInput = true,
    RedirectStandardOutput = true,
    Arguments = arguments,
    UseShellExecute = false,
    CreateNoWindow = false,
    WorkingDirectory = @"D:\AzCopy\bin\Debug\"
};

解法:

static void Main(string[] args)
    {
        string strCmdText = @"/c azcopy.exe sync ""D:\temp"" ""https://myhubforazcopy.blob.core.windows.net/myhubforazcopy1?sp=racwdl&st=2021-09-05T17:19:11Z&se=2021-09-06T01:19:11Z&spr=https&sv=2020-08-04&sr=c&sig=MAraJ0PxqJMDdYuWzrOUEwYda%2BkXEukP%2Fs%3D"" --delete-destination=true";
        
        CallProcess(strCmdText);
    }

 public static void CallProcess(string strCmdText)
    {
        var process = new Process();
        ProcessStartInfo startInfo = new ProcessStartInfo
        {
            FileName = @"cmd.exe",
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            Arguments = strCmdText,
            UseShellExecute = false,
            CreateNoWindow = false,
            WorkingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
        };


        process.StartInfo = startInfo;

        process.Start();

        process.StandardInput.Flush();
        process.StandardInput.Close();
        process.WaitForExit();
        Console.WriteLine(process.StandardOutput.ReadToEnd());
    }