changing a filename in C#

  • Thread starter Thread starter J. Marshall Latham
  • Start date Start date
J

J. Marshall Latham

I can't seem to find any resources to help me change a filename using C#. I
see a Path.ChangeExtension, but it only takes the full path as a string and
changes that string. It doesn't actually change the file (at least not from
what I have seen). I tried moving the file into the same directory with a
different name and totally lost my file. I am about to try moving it to
another directory and then moving it back with the new name. All I really
want to do is change the extension on the file from .tmp to .txt. Can
someone tell me how I can use C# code to change the name of a file. I
apologize if this is too elementary for this news group.

Thanks for your help,

JML
 
FileInfo original = new FileInfo(pathToFile);
if ( original.Exists ) {
// If the destination already exists then this won't copy.
// This is to protect other files when performing the operation.
original.CopyTo(Path.ChangeExtension(original.FullName, ".txt"), false);
}
 
J. Marshall Latham said:
I can't seem to find any resources to help me change a filename using C#. I
see a Path.ChangeExtension, but it only takes the full path as a string and
changes that string. It doesn't actually change the file (at least not from
what I have seen). I tried moving the file into the same directory with a
different name and totally lost my file. I am about to try moving it to
another directory and then moving it back with the new name. All I really
want to do is change the extension on the file from .tmp to .txt. Can
someone tell me how I can use C# code to change the name of a file. I
apologize if this is too elementary for this news group.

Have a look at File.Move.
 
I tried File.Move, but couldn't get it to work right for some reason. Maybe
my path was wrong. Thanks for the reply though.
 
Hi Marshall,

I think Justin's solution should work for you.

Anyway, if you still have further concern, please feel free to post, I will
help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Marshall,

Does my reply make sense to you? Is your problem resolved?

Please feel free to feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Jeffery,

I actually got it to work with the File.Move. I move it to a different
directory and then I move it to the desired directory with the new name. I
haven't had a chance to try what you and Justin were talking about. Sorry
about that. And thanks for the advice.
 
Hi Marshall,

Thanks for your feedback.

Ok, if you have any further problem, please feel free to feedback, I will
help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top