如何在c#中获取xcopy的输出 [英] how to get the output of xcopy in c#

查看:102
本文介绍了如何在c#中获取xcopy的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述







命令提示符使用以下命令返回xcopy命令的输出。



> xcopyd:\01-Oct-2013​​E:\/ e / y / I> xcopy.out





在特定位置创建的输出文件。





i需要C#代码中的输出文件。

$ / b


startInfo.Arguments =\+ SolutionDirectory +\++\+ TargetDirectory +\ + @/ e / y / I;



如何使用c#代码获取输出文件。



提前感谢....




command prompt returns the output for xcopy command using below command.

> xcopy "d:\01-Oct-2013" "E:\" /e/y/I >xcopy.out


the output file created in particular location.


i need the output file in C# code.


startInfo.Arguments = "\"" + SolutionDirectory + "\"" + " " + "\"" + TargetDirectory + "\"" + @" /e /y /I ";

how can i get the output file using c# code.

thanks in advance....

推荐答案

嗨sankmahesh,



尝试设置这些 startInfo 属性: UseShellExecute = false

RedirectStandardOutput = true 。然后你可以这样做:

Hi sankmahesh,

Try setting these startInfo properties: UseShellExecute = false and
RedirectStandardOutput = true. Then you can do something like:
myProcess.Start();
while (!myProcess.StandardOutput.EndOfStream) {
    string line = myProcess.StandardOutput.ReadLine();
    // do something with it (write to file, parse, or whatever you need to)
}





希望它有所帮助,

--V.Lorz



Hope it helps,
--V.Lorz


如果您遇到问题,有时候解决方案是将其反转。的确,为什么要使用xcopy呢?不,我明白问题很简单,你的方法很有效,你需要一个看似微不足道的细节。这很容易解决。不是那么快。您正在尝试创建单独的进程。并且这些流程很好地隔离,并且大多数应用程序不是为与其他流程协作而设计的。那么,为什么突破关门?



最直接的选择是使用可用的.NET FCL方法直接在代码中实现xcopy功能。您拥有所需的一切,只需要FCL方法:

http ://msdn.microsoft.com/en-us/library/c1sez4sc.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/07wt70x2.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/54a0at6s.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.io.file.copy.aspx [ ^ ]。



如你所见,一切都已经创建对于你。



但是,如果您仍想使用xcopy,解决方案是重定向 System.Diagnostics.Process.StandardOutput ,以防万一, System.Diagnostics.Process.StandardError 。你会在这里找到带有重定向的代码示例:

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx [ ^ ]。



祝你好运,

-SA
If the you face a problem, sometimes a solution is to invert it. Indeed, why using "xcopy" at all? No, I understand that the problem is simple, your approach works, and you need a seemingly minor detail. It is really easy to solve. Not so fast though. You are trying to create a separate process. And the processes are well isolated, and most of the applications are not designed for collaborations with other processes. So, why breaking through a closed door?

The most straightforward alternative is to implement xcopy functionality directly in your code using available .NET FCL methods. You have all you need, and you will need only for FCL methods:
http://msdn.microsoft.com/en-us/library/c1sez4sc.aspx[^],
http://msdn.microsoft.com/en-us/library/07wt70x2.aspx[^],
http://msdn.microsoft.com/en-us/library/54a0at6s.aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.file.copy.aspx[^].

As you can see, everything is already created for you.

However, if you still want to use xcopy, the solution is redirection of System.Diagnostics.Process.StandardOutput and, just in case, System.Diagnostics.Process.StandardError. You will find the code sample with redirection here:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx[^].

Good luck,
—SA


这篇关于如何在c#中获取xcopy的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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