[英]Using command prompt to delete specific files within downloads folder
我正在尝试使用以下代码从我的下载文件夹中删除特定文件-
var process = new Process();
var startInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Normal,
FileName = "cmd.exe",
RedirectStandardInput = true,
UseShellExecute = false
};
process.StartInfo = startInfo;
process.Start();
process.StandardInput.WriteLine("cd C://users/%username%/downloads");
process.StandardInput.WriteLine("del /f Secci*");
调试代码时-命令提示符窗口会闪烁,然后立即关闭(即使未指定要在代码中隐藏它),因此我一直在努力查看它是否甚至可以将CD放入正确的目录中。 当前,也没有从下载文件夹中删除文件。 这是测试自动化项目中“测试前”类的一部分。 如果有人可以提出一些建议,为什么这可能行不通,那就太好了吗?
用于在cmd提示符下删除。 尝试这个
string file = "Secci*";
Process process = new Process();
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.UseShellExecute = false;
process.Start();
process.StandardInput.WriteLine("cd C://users/%username%/downloads");
process.StandardInput.WriteLine(string.Format("del \"{0}\"", file));
如果您尝试使用System.IO,请尝试此操作。
using System.IO;
string file = "Secci*";
//Because "SpecialFolder" doesn't have Downloads in it, this is my workaround. There may be better ones out there.
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
path = path.Replace("Documents", "Downloads");
string[] List = Directory.GetFiles(path, file);
foreach (string f in List)
{
File.Delete(f);
}
您可以通过列举目录来获取所有文件。 获得符合条件的文件后,就可以对其进行遍历并对其执行操作。
var dir = new DirectoryInfo("C://users/%username%/downloads");
foreach (var file in dir.EnumerateFiles("Secci*")) {
file.Delete();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.