如何在GUID模式表单中列出文件 [英] How to list files in the GUID pattern form
本文介绍了如何在GUID模式表单中列出文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于某些代码,许多文件格式为
acd5604f-5bbd-4e94-9b40-ea0247cdb4ad
已在某些客户端文件夹中创建。这些文件是使用一些GUID()函数创建的,它们没有扩展名。
我需要删除它们,有什么模式可以帮助以最简单的方式获取它们。
解决方案
您可以使用Guid.TryParse方法
[ ^ ]
string filename = acd5604f-5bbd-4e94-9b40-ea0247cdb4ad;
Guid guid;
if (Guid.TryParse(filename, out guid))
{
// 删除文件
}
模式是:
- 8个十六进制数字
- 破折号
- 4个十六进制数字
- 破折号
- 4个十六进制数字
- 破折号
- 4个十六进制数字
- 破折号
- 12个十六进制数字
正则表达式可以轻松捕获:
正则表达式r = nex正则表达式( @ ^ [0-9a-zA-Z] {8} - [0 -9a-ZA-Z] {4} - [0-9A-ZA-Z] {4} - [0-9A-ZA-Z] {4} - [0-9A-ZA-Z] {12}
string fileName;
foreach (FileInfo fi in dir.GetFiles()){
fileName = fi。文件名;
if (r.IsMatch(fileName)){ // 这里文件名是GUID
fi.Delete();
}
}
due to some code many files of the form
"acd5604f-5bbd-4e94-9b40-ea0247cdb4ad"
have been created in some of the clients' folders. These files are created using some GUID() function and they have no extension.
I needed to remove them, what is the pattern that helps getting them in the easiest way.
解决方案
you can useGuid.TryParse Method
[^]
string filename = "acd5604f-5bbd-4e94-9b40-ea0247cdb4ad"; Guid guid; if(Guid.TryParse(filename , out guid)) { //delete file }
The pattern is:
- 8 hexadecimal digits
- dash
- 4 hexadecimal digits
- dash
- 4 hexadecimal digits
- dash
- 4 hexadecimal digits
- dash
- 12 hexadecimal digits
A regular expression could catch it easily:
Regex r = nex Regex(@"^[0-9a-zA-Z]{8}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{12}
" string fileName; foreach (FileInfo fi in dir.GetFiles()) { fileName = fi.FileName; if (r.IsMatch(fileName)) { // Here the file name is a GUID fi.Delete(); } }
这篇关于如何在GUID模式表单中列出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文