Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
I would like to read a file from the drive, but it gives me an error while reading the file.
Here is my code below:
                lblLastReal.Text = e.FullPath;
                listBox3.Items.Add(lblLastReal.Text);
                openFileDialog1.FileName = "";
                TextBox txtScanBox = new TextBox();
               
                StreamReader stread = new StreamReader(System.IO.Directory.GetCurrentDirectory() + @"\abc.txt");
                txtScanBox.Text = stread.ReadToEnd(); 
                MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
                GC.Collect();
               
                File.SetAttributes(e.FullPath, FileAttributes.ReadOnly);
                FileStream fs = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, 0x2000);
                fs = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, 0x2000);
                MD5.ComputeHash(fs);
                byte[] hash = MD5.Hash;
                StringBuilder buffer = new StringBuilder();
                byte hashByte = 0;
                foreach (byte hashByte_loopVariable in hash)
                {
                    hashByte = hashByte_loopVariable;
                    buffer.Append(string.Format("{0:X2}", hashByte));
                }
                fs.Close();
and sometimes it gives me error "The process cannot access the file because it is being used by another process"
Posted 3-Dec-12 1:33am
Edited 3-Dec-12 1:40am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The second error first: "The process cannot access the file because it is being used by another process"
Try closing your StreamReader stread - since you just let it go out of scope, it will only be closed when the Garbage Collector decides it is necessary to remove it.
 
The first problem is probably a permissions issue - without knowing what "e" is in your code fragment, I can't say exactly, but the chances are that whatever user your application is running as does not have the required permissions for the folder you are trying to set the file attributes on.
  Permalink  
Comments
RaisKazi at 3-Dec-12 15:07pm
   
Op's comments -
thnx for ur suggestion,
i have checked with all posibility of closing fs(streamreader) but didn't work
e is the variable of filesystemwatcher changed event.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

thnx for ur suggestion,
i have checked with all posibility of closing fs(streamreader) but didn't work
e is the variable of filesystemwatcher changed event.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 3 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100