C#中的Xcopy - 如何调用它?

时间:2012-12-09 15:39:08

标签: c#

这里我应该将文件从附近的本地IP地址复制到我的本地系统我已经使用以下代码使用Xcopy命令进行复制,然后启动该过程但是如果执行命令则复制代码中的Argumentsetting提示它是复制但是通过代码不复制请告诉我们是什么问题。有任何想法吗?什么通过代码不复制。

string Porocess = String.Format("\"{0}\\xcopy.exe\"", Environment.SystemDirectory.ToString());

string SolutionSettings = string.Format("\"\\\\{0}\\C$\\Documents and Settings\\All Users\\Application Data\\Symantec\\Common Client\\settings.bak\"", IPaddress);

string TargetSettings = string.Format("\"C:\\Documents and Settings\\All Users\\Application Data\\Symantec\\settings.bak\"");

string Argumentsetting = /*"\"" +*/ SolutionSettings + " " + TargetSettings + " /Y";// parameters to launch process

int iret1 = LauncProcess(Porocess, Argumentsetting, Environment.SystemDirectory.ToString());



 public static int LauncProcess(string sProcess, string sParams, string sWorkingDir)
        {
            int iRet = 0;
            Process process = null;

            try
            {

                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = sProcess;
                startInfo.Arguments = sParams;
                startInfo.WorkingDirectory = sWorkingDir;
                startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                startInfo.CreateNoWindow = true;

                startInfo.UseShellExecute = false;


                process = Process.Start(startInfo);

                process.WaitForExit();

                Console.WriteLine("Copy has finished.");

                if (process.HasExited)
                {
                    Console.WriteLine("Process has exited.");
                    if (process.ExitCode != 0)
                    {

                        iRet = 1;

                    }
                    else
                    {
                        iRet = 0;
                    }
                }
                else
                {

                    iRet = 1;

                }
            }
            catch (Exception ex)
            {

                iRet = 1;
            }
            finally
            {
                process.Dispose();
            }
            return iRet;
        }

1 个答案:

答案 0 :(得分:0)

检查源地址和目的地地址是否正确给出

  Process process = new Process();
  process.StartInfo.Arguments= @"D:\sourcePath F:\DestinationPath /e /y /I";
  process.Start();