Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Im using this code to copy the image...
but when update the image, i want to first delete this image, then copy new image... it is work well...

but when form is close and again open the form then it is display the Error like : it is being use by another process. so how can i solve this error.


C#
using (DataTableAdapters.FilePathTableAdapter Path = new global::SmartEducationSystem.DataTableAdapters.FilePathTableAdapter())
                                {
                                    DataTable dt1 = Path.FilePath_GetDataByName("Photo Path");
                                    if (dt.Rows.Count > 0)
                                    {
                                        string P_Path = dt1.Rows[0]["Path"].ToString() + "Logo.jpg";

                                        try
                                        {
                                            System.IO.File.Delete(P_Path);
                                            System.IO.File.Copy(PhotoPath + @"\" + filename, P_Path, true);
                                        }
                                        catch (UnauthorizedAccessException dirNotFound)
                                        {
                                            Console.WriteLine(dirNotFound.Message);
                                        }

                                        imgpath = P_Path;
                                    }
                                }




it is being used by another process...
Posted

1 solution

This is not your main problem; you main problem is understanding. The notion of "Form" is related to UI. How a UI can be related to such thing as file/stream operations? You need to embrace separation of concerns.

Now, let's get to the problem of the file access.

It depends on how you opened the file. Most likely, you forgot to dispose something. The disposal mechanism usually takes care of certain things you need to clean up in a controlled manner (in contrast to, say, Garbage Collector functionality), in some point of execution of your code; and some typical uses of it are cleaning up unmanaged resources and — what a surprise! — closing of file handles.

Basically, you need to watch yourself of using of any types implementing System.IDisposable and call System.IDisposable.Dispose in the appropriate points. Please see:
http://msdn.microsoft.com/en-us/library/system.idisposable.aspx[^].

It's important to makes sure this call is done even if exception is thrown, via a try-finally statement. The best fool-proof way of doing it is using the using statement (don't mix it up with using clause!). Please see:
http://msdn.microsoft.com/en-us/library/yh598w02.aspx[^].

Let's consider a simple example. When you open a file with System.IO.StreamReader, you do it in the exclusive-use mode (which is the best thing to do anyway). You won't be able to read the same file in parallel until the instance of the reader is disposed:

C#
// you can access the file fileName before the next line
using (System.IO.StreamReader reader = new System.IO.StreamReader(fileName)) {
    // from this moment, file access is blocked; you can only read it with this reader
    string line = reader.ReadLine();
    //...
} //reader.Dispose is called here automatically
// and here you can access the file


You could have used different ways of accessing your files, you could get read, write, read/write access, work on lower levels of file or stream interfaces, etc., but the principle is the same.

Where do you have this problem? This is easy to investigate.

First of all, the similar question was asked here many times, and from this experience I know: in most cases the blocking process is your own process. You could have forgotten to dispose/close something in the same application. So, first of all, check it up, as explained above.

In some cases, you really need to investigate which process holds which file. For this, I recommend using one utility from the Sysinternals Suite. This set of utilities (formerly from Winternals company, presently at Microsoft) is a must-have for any developer, please see:
http://technet.microsoft.com/en-us/sysinternals/bb842062[^],
http://technet.microsoft.com/en-us/sysinternals/bb545027[^].

The utility you need is "handle.exe", please see:
http://technet.microsoft.com/en-us/sysinternals/bb896655[^].

In your case, you use it with file name parameter:
handle.exe <file_name>


This utility will scan all kinds of handles, not just file handles. For file, it will scan all file handles matching the file name (so it does not have to be a full path name) and return information sufficient to identify each process, including its pid. So, if you need more information on a process in question, you can also use other Sysinternals utilities, in particular, its Process Explorer:
http://technet.microsoft.com/en-us/sysinternals/bb896653[^].

Good luck,
—SA
 
Share this answer
 

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