Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends ,

I have replace the file name..
C#
string pfname = @"E:\Image\" + filename + ".jpg";
           string BackUp=@"E:\BackupImage\" + filename + "1.jpg";
           MemoryStream ms = new MemoryStream(image);
           Image returnImage = Image.FromStream(ms);
           FileInfo ffi = new FileInfo(pfname);
           if (ffi.Exists)
           {
               //System.IO.FileInfo file = new System.IO.FileInfo(pfname);
               //System.IO.FileInfo f = file.CopyTo(pfname, true);
               //file.Delete();
              ReplaceFile(pfname, pfname, BackUp);
              // System.IO.File.Delete(pfname);
               //System.IO.F
           }
           returnImage.Save(@"E:\Image\" + filename + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

C#
public static void ReplaceFile(string FileToMoveAndDelete, string FileToReplace, string BackupOfFileToReplace)
    {
        File.Replace(FileToMoveAndDelete, FileToReplace, BackupOfFileToReplace, false);

    }


I have the big problem. if file name is exits -

I get the error for.
The process cannot access the file because it is being used by another process.

How to solved this.

Note : I cant use file. Delete function.


Regards.
sathya
Posted
Updated 16-Mar-12 13:57pm
v2

1 solution

dispose the memory stream then call delete function.



C#
if (ffi.Exists)
          {
ms.Close();
              //System.IO.FileInfo file = new System.IO.FileInfo(pfname);
              //System.IO.FileInfo f = file.CopyTo(pfname, true);
              //file.Delete();
             ReplaceFile(pfname, pfname, BackUp);
             // System.IO.File.Delete(pfname);
              //System.IO.F
          }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900