windows 从远程计算机删除文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/231259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete files from remote computer
提问by Ryan Rodemoyer
I'm trying to delete a directory that contains XML files from a remote computer. My code compiles and runs fine, but when I go to get a list of XML files in the path I specify, it is not returning anything. Am I missing something permission wise?
我正在尝试从远程计算机中删除包含 XML 文件的目录。我的代码编译并运行良好,但是当我在指定的路径中获取 XML 文件列表时,它没有返回任何内容。我是否遗漏了一些许可?
I have ran it from my computer logged on as myself and from another computer logged on as a different user. Both accounts have full control over the directory that contains the XML files.
我已经从以我自己身份登录的计算机和以不同用户身份登录的另一台计算机上运行了它。这两个帐户都可以完全控制包含 XML 文件的目录。
I'm using .NET 2.0.
我正在使用 .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());
}
}
}
回答by Travis Collins
I think you should be using *.xml instead of simply .xml. But I also concur with Kyralessa, test on your local machine first, then add in the complexity of going across a network.
我认为您应该使用 *.xml 而不是简单的 .xml。但我也同意 Kyralessa,首先在您的本地机器上进行测试,然后添加通过网络的复杂性。
回答by Brett McCann
in DeleteFiles, you have the following line:
在 DeleteFiles 中,您有以下行:
fi = di.GetFiles(ext);
fi = di.GetFiles(ext);
where ext is the extension you pass in, which I believe is just '.xml'. Get files is looking for any files called '.xml'. GetFiles takes wildcards, which I believe is what you are intending to do. Put an asterisk (*) at the front and give that a try.
其中 ext 是您传入的扩展名,我认为它只是“.xml”。获取文件正在寻找任何名为“.xml”的文件。GetFiles 采用通配符,我相信这是您打算做的。在前面放一个星号 (*) 并尝试一下。
-Brett
-布雷特
回答by Ryan Rodemoyer
Follow up:
跟进:
I needed to use *.xml (should have known that!) and now it works.
我需要使用 *.xml(应该知道!)现在它可以工作了。
This site is great!
这个网站很棒!
回答by Ely
I assume you are passing in a network path? Does it fail when you run the program on a local path? Does this line: fi = di.GetFiles(ext); Return any fileInfo objects?
我假设您正在传递网络路径?在本地路径上运行程序时会失败吗?这行是否:fi = di.GetFiles(ext); 返回任何文件信息对象?
You probably just have something small wrong that can be fixed by some debugging.
你可能只是有一些小错误可以通过一些调试来修复。
回答by Ryan Abbott
What are you passing in as the argument? Are you using a Mapped Drive or the direct reference (i.e. //server/folder)?
你传入的参数是什么?您使用的是映射驱动器还是直接引用(即//服务器/文件夹)?
Instead of your ValidateDirectory, you should use Directory.Exists(directory) just to see if it can see the directory at all.
您应该使用 Directory.Exists(directory) 而不是您的 ValidateDirectory 来查看它是否可以看到该目录。