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

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

user3002030
  • 149
  • 1
  • 2
  • 11
  • 6
    There shouldn't be any problem if the file doesn't have any extension. Note that you should write as `File.Exists(@"`, because otherwise the various `\\` will be considered escape characters. – xanatos Jun 19 '15 at 07:48
  • @xanatos: The question is pretty clear, isn't it? He wants to find files without extensions and give them extensions. At least that is how i understand it. – Tim Schmelter Jun 19 '15 at 08:01
  • 1
    @TimSchmelter A textual reading of his question doesn't include anything of what you wrote. He wrote *The file test has no extension and I need help to either move or rename this file to something with a extension*. I don't see any "I want to find all the files without extension" or something similar. – xanatos Jun 19 '15 at 08:02
  • @xanatos: the longer i look at it the more i agree with you. I don't know why it was so clear to me that he wants to change the fact that one or multiple files don't have an extension. Maybe because there was a similar question today or because i found it interesting. – Tim Schmelter Jun 19 '15 at 08:07

3 Answers3

8

Having no extension has no bearing on the function. Also, a rename is really just a move "in disguise", so what you want to do is

File.Move(@"C:\Users\Username\Desktop\test", @"C:\Users\Username\Desktop\potato.txt")

Please bear in mind the @ before the string, as you haven't escaped the backslashes.

Dasanko
  • 579
  • 8
  • 16
  • And if you need to determine the proper extension you can take a look at http://stackoverflow.com/a/58570/1498533 (detects only on 26 mime types but it's a beginning) – Damien Jun 19 '15 at 08:10
1

There's nothing special about extensionless files. Your code is broken because you use string concatenation to build a path and you're mixing verbatim and regular string literal syntax. Use the proper framework method for this: Path.Combine().

string fullPath = Path.Combine(@"C:\Users", Environment.UserName, @"Desktop\test");

if(File.Exists(fullPath))
{

}

You also should use the proper framework method to get the desktop path for the current user, see How to get a path to the desktop for current user in C#?:

string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

string fullPath = Path.Combine(desktopPath, "test");

Then you can call File.Move() to rename the file, see Rename a file in C#:

if(File.Exists(fullPath))
{
    string newPath = fullPath + ".txt";     
    File.Move(fullPath, newPath);
}
Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
0

You can get all files without extension in this way:

var files = Directory.EnumerateFiles(@"C:\Users\Username\Desktop\")
    .Where(fn => string.IsNullOrEmpty(Path.GetExtension(fn)));

Now you can loop them and change the extension:

foreach (string filePath in filPaths)
{
    string fileWithNewExtension = Path.ChangeExtension(filePath, ".txt");
    string newPath = Path.Combine(Path.GetDirectoryName(filePath), fileWithNewExtension);
    File.Move(filePath, newPath);
}

As you can see, the Path-class is a great help.


Update: if you just want to change the extension of a single file that you already know it seems that Dasanko has already given the answer.

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 1
    You can enumerate extensionless files with: `Directory.EnumerateFiles(@"C:\Users\Username\Desktop\", "*.")` – xanatos Jun 19 '15 at 08:04
  • @xanatos: that's even simpler, didn't know it. I'm suprised that there is no question on SO with this topic. This only seems so: http://stackoverflow.com/questions/6336058/c-sharp-get-all-file-names-without-extension-from-directory – Tim Schmelter Jun 19 '15 at 08:30