Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在没有文件扩展名的情况下检查文件是否存在?C#_C#_File_Io_Exists - Fatal编程技术网

如何在没有文件扩展名的情况下检查文件是否存在?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的问题,你只是回答了标题。