[英]How to move files from one path to another in c#
In my application I could have three paths 在我的应用程序中,我可以有三个路径
This file paths will come in variable. 该文件路径将是可变的。
Now How can I check if path has single file or multiple or it has wildcard ? 现在,如何检查路径是单个文件还是多个文件,还是通配符? & then move them to another path.
然后将它们移到另一条路径。
Well, since neither *
nor ?
好吧,因为既不是
*
也不是?
can be in the path: they are in 可以在路上:他们在
Char[] forbidden = Path.GetInvalidPathChars();
so you can just look for them 所以你可以找他们
String path = @"C:\MyData\Shared\*.txt";
...
Boolean isWildCard = path.ContainsAny('?', '*');
As for File/Directory 至于文件/目录
Boolean isFile;
if (File.Exists(path))
isFile = true; // file already exists
else if (Directory.Exists(path))
isFile = false; // directory already exists
else if (String.Equals(Path.GetExtension(path), ".txt", StringComparison.InvariantCultureIgnoreCase))
isFile = true; // has txt extension, let it be a file
else
isFile = false;
However it seems that you have no need to have any branches (isWildCard, isFile) and just move files: 但是,似乎您不需要任何分支(isWildCard,isFile),只需移动文件即可:
String path = @"C:\MyData\Shared\*.txt";
...
String sourceDirectory = Path.GetDirectoryName(path);
String destination = @"C:\Destination";
Directory.GetFiles(sourceDirectory, "*.txt")
.ForEach(file => File.Move(file, Path.Combine(destination, Path.GetFileName(file))));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.