我正在尝试从远程计算机中删除包含 XML 文件的目录。我的代码编译并运行良好,但是当我在我指定的路径中获取 XML 文件列表时,它没有返回任何内容。我错过了什么许可吗?
我已经从以我自己身份登录的计算机和以其他用户身份登录的另一台计算机运行它。两个帐户都可以完全控制包含 XML 文件的目录。
我正在使用.NET 2.0。
static void Main(string[] args) {
string directory, ext = ".xml"; // have tried xml and .xml
if (args.Length != 1) {
// do absolutely nothing if we do not exactly 1 argument
} else {
Console.WriteLine("Argument accepted.");
directory = args[0];
// make sure the directory passed is valid
if (ValidateDirectory(directory)) {
Console.WriteLine("Directory is valid.");
DeleteFiles(directory, ext);
}
}
Console.WriteLine("Done.");
}
static bool ValidateDirectory(string d) {
return Regex.IsMatch(d, @""); // I removed my regex - it validates properly
}
static void DeleteFiles(string d, string ext) {
DirectoryInfo di;
FileInfo[] fi;
di = new DirectoryInfo(d);
fi = di.GetFiles(ext);
Console.WriteLine("Number of files = " + fi.Length + ".");
foreach (FileInfo f in fi) {
try {
Console.WriteLine(f.FullName);
f.Delete();
} catch (Exception ex) {
// do nothing when there is an exception
// just do not want it to quit
Console.WriteLine(ex.ToString());
}
}
}