如何在没有文件扩展名的情况下检查文件是否存在?C#
文件测试没有扩展名,我需要帮助将此文件移动或重命名为具有扩展名的文件如何在没有文件扩展名的情况下检查文件是否存在?C#,c#,file,io,exists,C#,File,Io,Exists,文件测试没有扩展名,我需要帮助将此文件移动或重命名为具有扩展名的文件 if(File.Exists(@"C:\\Users" + Environment.UserName + "\\Desktop\\test")) { /\ this file has no file exte
if(File.Exists(@"C:\\Users" + Environment.UserName + "\\Desktop\\test"))
{ /\
this file has no file extension
}
试着这样做:
DirectoryInfo d = new DirectoryInfo("directory path");
FileInfo[] f = d.GetFiles("test.*");
if (f.Length > 0)
{
File.Move(oldPath, newPath);
}
else
{
//File does not exist
}
同时选中使用目录信息获取目录中的文件列表,然后对于那些没有扩展名的文件,将其删除。我在文件夹
M:\Incoming
中创建了一个名为test
的文件,没有扩展名
运行以下代码在这两种情况下都有效:
if (File.Exists(@"M:\Incoming\test"))
Console.WriteLine("Exists");
if (File.Exists(@"M:\\Incoming\\test"))
Console.WriteLine("Exists");
当使用@
时,您不需要指定两个斜杠,尽管在本例中没有任何区别
输出:
存在
存在
您的问题很可能是串接字符串的方式。您忘记了
“C:\\Users”之后的“\\\”
此外,在使用@”时,您不需要双重转义反斜杠。不要发布重复的问题请使用System.IO.Path.Combine。还有一种获得桌面目录的推荐方法:Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)“像这样尝试”不是答案。你也没有试着去理解OP的问题,你只是回答了标题。