The reprinted file is being used by another process and cannot be accessed by this process.

Source: Internet
Author: User

The reprinted file is being used by another process and cannot be accessed by this process.

Address: http://www.cnblogs.com/enjoyprogram/p/4344799.html

 

Exception prompt: "The file is being used by another process and the process cannot access the file ". After opening a file, try to open the file again, or if you want to perform other operations on the file after opening the file, this error message may occur. This error is usually caused by a problem in parameter settings when System. IO. FileStream is constructed. Usually used directly: FileStream fs = new FileStream (fileName, FileMode. open) This method is used to Open a file in read-only sharing mode. However, if the file has been opened by a process with write permission, it cannot be read, therefore, FileStream fs = new FileStream (fileName, FileMode. open, FileAccess. read, FileShare. readWrite); set the file sharing mode to read/write: FileShare. readWrite. This will solve the problem.

 

If you want to delete or overwrite an image file after you load it to Picturebox, the above error message is displayed.

PictureBox1.Image = Image. FromFile (sFile); // This method is used to load the Image. After the page is closed, the file in this path may still be occupied by the thread and cannot be deleted or overwritten.

 

PictureBox1.ImageLocation = sFile;


FileStream fs = new FileStream (sFile, FileMode. Open, FileAccess. Read, FileShare. ReadWrite );
Byte [] bytes = new byte [fs. Length];
Fs. Read (bytes, 0, bytes. Length );
Fs. Close ();
MemoryStream MS = new MemoryStream (bytes );
Bitmap img = new Bitmap (MS );
PictureBox1.Image = img;

The file will no longer be occupied.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.