Community discussion forum

File Deletion

  • 1 year ago

    Hi, 

    I tried to delete an exiting file from the application using C#. The statement is as follows:

    bool fileExists = System.IO.File.Exists(fileName);

    if (fileExists == true)

    {

    System.IO.File.Delete(fileName);

    }

     

    But i"m getting the exception like:

    The process cannot access the file because it is being used by another process.

    Can anyone give the solution to access the file and delete without any exceptions.

     

    Thanks in advance,

    Murali

  • 1 year ago

    Hi Murali,

     It will be better u use StreamReader IO objects and access the file. You write a code to open ur file in Share Mode which has to be deleted by the reader object and then close the file. Then you delete it. This will have acess control on that file to delete. And make sure the file you are deleting has proper permission set.

    Regards,

    Moiz

Post a reply

Enter your message below

Sign in or Join us (it's free).

Want to stay in touch with what's going on? Follow us on twitter!