Re: changing a filename in C#

From: Justin Rogers (Justin_at_games4dotnet.com)
Date: 03/06/04


Date: Fri, 5 Mar 2004 22:46:06 -0800

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);
}

-- 
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"J. Marshall Latham" <marshall@networkip.net> wrote in message
news:uDAhaB0AEHA.1516@TK2MSFTNGP10.phx.gbl...
> 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
>
>


Relevant Pages

  • Re: IP address blocks
    ... the string "vc" which I took to be either Victoria ... interface, of course, which is what they claim their ISP does. ... I'm sure their lips are moving as they read too. ...
    (rec.crafts.glass)
  • Re: Stop gnome-terminal from clearing screen
    ... By "this" you are talking about the proposed sledge-hammer way of faking ... a vt220 terminal by moving and symlinking? ... and the comments to that blog are much more informative than the ...
    (GNOME)
  • Re: Luthier question: Intonating via thinning braces
    ... Top motion can effect the pitch of specific partials of certain notes. ... The same thing happens when the string is pushing on the top: ... position, and moving downward, and pulling 'down' when it's below. ...
    (rec.music.classical.guitar)
  • Re: Delimited String to Array
    ... The guy said that he was moving from delphi to C# and was working with ... string. ... Perhaps he could also glean additional information from the errata ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: SHFileoperation question
    ... Here's some routine i've used in a distributed application. ... If the path contains only a folder then you're moving a folder ... If the path contains a filename, then you're moving a file etc... ... BOOL MoveFolder(string &from, string &to) ...
    (microsoft.public.vc.language)