File Deletion

  • 9 years 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

  • 9 years 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).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The trouble with programmers is that you can never tell what a programmer is doing until it's too late.” - Seymour Cray