C# wpf 列出文件夹所有文件
在网上找了
cmd输入
dir “要列出的文件夹*.*” /a /b /s>”要输出的文件”
可以重定向把文件夹内容输出到文件
tree “要列出的文件夹” /f>”要输出的文件”
可以树列出文件夹内容输出文件
因为每次打这命令难,做成一个程序
核心
public void 列出路径()
{
string str = "dir \"" + 要列出的文件夹 + "\\*.*\" /a /b /s>\"" + 要输出的文件 + "\"";
cmd输入(str);
}
public void 列树文件()
{
string str="tree \"" + 要列出的文件夹 + "\" /f>\"" + 要输出的文件 + "\"";
cmd输入(str);
}
private void cmd输入(string str)
{
try
{
console = new System.Diagnostics.Process();
console.StartInfo.FileName = "cmd.exe";
console.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
console.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
console.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
console.StartInfo.RedirectStandardError = true;//重定向标准错误输出
console.StartInfo.CreateNoWindow = true;//不显示程序窗口
console.Start();//启动程序
//向cmd窗口发送输入信息
console.StandardInput.WriteLine(str + "&exit");
console.StandardInput.AutoFlush = true;
//p.StandardInput.WriteLine("exit");
//向标准输入写入要执行的命令。这里使用&是批处理命令的符号,表示前面一个命令不管是否执行成功都执行后面(exit)命令,如果不执行exit命令,后面调用ReadToEnd()方法会假死
//同类的符号还有&&和||前者表示必须前一个命令执行成功才会执行后面的命令,后者表示必须前一个命令执行失败才会执行后面的命令
//获取cmd窗口的输出信息
string output = console.StandardOutput.ReadToEnd();
//StreamReader reader = p.StandardOutput;
//string line=reader.ReadLine();
//while (!reader.EndOfStream)
//{
// str += line + " ";
// line = reader.ReadLine();
//}
System.Diagnostics.Process.Start("explorer.exe" , 要输出的文件);
console.WaitForExit();//等待程序执行完退出进程
console.Close();
}
catch
{
}
}
private System.Diagnostics.Process console;
代码https://code.csdn.net/lindexi_gd/file_folder_file
参考文献:http://www.cnblogs.com/babycool/p/3570648.html C#使用cmd
博客园博客只做备份,博客发布就不再更新,如果想看最新博客,请访问 https://blog.lindexi.com/
如图片看不见,请在浏览器开启不安全http内容兼容
![知识共享许可协议](https://licensebuttons.net/l/by-nc-sa/4.0/88x31.png)
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名[林德熙](https://www.cnblogs.com/lindexi)(包含链接:https://www.cnblogs.com/lindexi ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我[联系](mailto:lindexi_gd@163.com)。