更新时间:2022-10-19 08:01:27
您可以使用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(); } }