Connecting Tech Pros Worldwide Forums | Help | Site Map

rename file in c#

Newbie
 
Join Date: May 2006
Posts: 2
#1: May 2 '06
i use:
fiFileInfo.CopyTo(Path.ChangeExtension(fiFileInfo. FullName + fiFileInfo.Extension, ".enc"), false);
to rename a file (eg: crap.ppt -> crap.ppt.enc).

if the file is really big (400mb), this take 6-7 minutes. is there a better way to rename files?

regards
meeeeeeeeeep
best answer - posted by Banfa
try

fiFileInfo.MoveTo( Path.ChangeExtension(fiFileInfo.FullName + fiFileInfo.Extension, ".enc") )

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,206
#2: May 2 '06

re: rename file in c#


try

fiFileInfo.MoveTo( Path.ChangeExtension(fiFileInfo.FullName + fiFileInfo.Extension, ".enc") )
Newbie
 
Join Date: May 2006
Posts: 2
#3: May 3 '06

re: rename file in c#


cheers, and im sorry, i should have known that..
Newbie
 
Join Date: Jan 2007
Posts: 3
#4: Jan 29 '07

re: rename file in c#


It is better to use method
Path.ChangeExtension(string path, string extension);
Newbie
 
Join Date: Oct 2007
Posts: 1
#5: Oct 16 '07

re: rename file in c#


OK here the easist way with all descriptions:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. namespace rhozetApplication3
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.  
  18.         private int File_Delete(string Filepath)
  19.         {
  20.             FileInfo fi = new FileInfo(Filepath);
  21.  
  22.             if (fi.Exists)
  23.             {
  24.                 fi.Delete();
  25.             }
  26.             return 1;
  27.         }
  28.  
  29.         private int File_rename(string FilepathOld, string FilepathNew)
  30.         {
  31.             FileInfo fi = new FileInfo(FilepathOld);
  32.  
  33.             if (fi.Exists)
  34.             {
  35.                 fi.MoveTo(FilepathNew);
  36.  
  37.             }
  38.             return 1;
  39.         }
  40.  
  41.         private void button1_Click(object sender, EventArgs e)
  42.  
  43.         {
  44.  
  45.             FileInfo fi = new FileInfo("C:\\dateiname.txt");
  46.  
  47.             if (fi.Exists)
  48.             {
  49.                 fi.MoveTo("C:\\dateiname.mpg");
  50.                 textBox1.Text = "           Succesfull!    ";
  51.             }
  52.             else
  53.             {
  54.                 textBox1.Text = "           Failed!    ";
  55.             }
  56.         }
  57.     }}
\\ a button and a text box is requiert to run it

have fun
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,784
#6: 3 Weeks Ago

re: rename file in c#


TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Reply