Click here to Skip to main content
14,978,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I have a registration form in which all details are enter with photo of user.
now all details are bind in datagridview. now on click of datarow data are bind into there field for edit,photo is also bind.

i do to process with datarow click, user edit or print there data with photo.
but when i go for print the i get an error...

"The process cannot access the file 'D:\rs\rAHUL\test1\Work_Station\bin\Debug\MemberImages\1.JPG' because it is being used by another process."

i applied this code

C#
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "MemberImages\\" + img))
               {
                   fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "MemberImages\\" + img, FileMode.Open);
               }
               else
               {
                   fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "MemberImages/NoPhoto.jpg", FileMode.Open);
               }



so please tell me how to fixed this problem.
Posted
Comments
AnthonyMG 2-Dec-13 1:12am
   
Did you check if the file that your accessing is being opened already..?
jackspero18 2-Dec-13 1:21am
   
Its mean the image still not uploaded to the server.
its in process of uploading.
after uploading 100% then you can access that file

should added the FileAccess and FileShare tag.
C#
fs = new FileStream (zipFilepath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)
   
Comments
   
Yes, it will work, but this is not a right advice, in most cases. Usually, the file should not be shared, but it should be closed properly after use.
—SA
NWPU_Tbeck 2-Dec-13 3:13am
   
How do you make sure file is not used by another process? Very interest it.
As this file is an image file, first take a look at the recent discussion: how delete file after use Image.FromFile() in c#[^].

From my experience, the most usual case of blocking a file is blocking it by the same process. When some file is opened, it can be opened for exclusive singe use, or for shared use. By default, and in most cases, files are opened exclusively. In your code, you don't show where and how you close the file handles, and it can be a problem.

Fortunately, this situation is easy enough to investigate. Please see my past answers:
File used by another process exception[^],
how to compress the error 'it is already used by another process' in vb.net[^].

—SA
   

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