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
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
Please bear in mind the @ before the string, as you haven't escaped the backslashes.
You can get all files without extension in this way:
Now you can loop them and change the extension:
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.
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()
.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#?:
Then you can call
File.Move()
to rename the file, see Rename a file in C#: