-1

The file test has no extension and I need help to either move or rename this file to something with a extension

if(File.Exists(@"C:\\Users" + Environment.UserName + "\\Desktop\\test"))
{                                                                 /\
                                               this file has no file extension
}
user3002030
  • 149
  • 1
  • 2
  • 11

3 Answers3

1

I created a file named test with no extension in the folder M:\Incoming.

Running the following code works in both cases:

if (File.Exists(@"M:\Incoming\test"))
    Console.WriteLine("Exists");

if (File.Exists(@"M:\\Incoming\\test"))
    Console.WriteLine("Exists");

When using @ you do not need to specify two slashes, although it makes no difference anyway in this example.

Output:

Exists

Exists

Your problem is most likely to be in the way in which you are concatenating the strings.

Community
  • 1
  • 1
Martin
  • 16,093
  • 1
  • 29
  • 48
0

Try like this:

DirectoryInfo d = new DirectoryInfo("directory path");
FileInfo[] f = d.GetFiles("test.*");
if (f.Length > 0)
{
   File.Move(oldPath, newPath);
}
else
{
  //File does not exist
}

Also check Directory.GetFiles

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • "Try like this" is not an answer. You also didn't try to understand OP's problem, you just answered the title. – CodeCaster Jun 19 '15 at 10:42
-1

Use the directory info to get the list of files in directory, then for those files without extension, remove them.

Jegan
  • 1,227
  • 9
  • 16