Im getting exception AccessDenied since the file is in use by another program when try to open the f

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have this code :


<pre class="prettyprint _fi = new DirectoryInfo (subDirectoryName).GetFiles("*.bmp");
Dictionary<string, FileInfo> fileDict = _fi.ToDictionary(f => f.Name);
label15.Text = _fi.Length.ToString();
for (int i = 0; i < myNumbers.Count; i++)
{
if (myNumbers >= max_min_threshold)
{


if (i == _fi.Length)
{
break;
}
FileName = (i + 1).ToString("D6") + ".bmp";
if (fileDict.TryGetValue(FileName, out _fi))
{

//}
//if (File.Exists(subDirectoryName + "\" + FileName))
//{[/code]
_fi is FileInfo[] and myNumbers is a List.
_fi contain 2255 indexs in each one a file name for example in index[0] there is {000001.bmp}
In index[1] there is {000002.bmp} and so on.
Now myNumbers contain 2256 indexs in each one a number for example in index[0] 255000 in index[1] 254000 and so on.
Now i wanted to put in the variable FileName each itertion the next file name from the _fi variable.
The problem is that the _fi and myNumbers not the same number of indexs. So i did :

<pre class="prettyprint if (i == _fi.Length)
{
break;
}
FileName = (i + 1).ToString("D6") + ".bmp";[/code]
Since if i will not do the break; i will get exception error on the FileName out of index...
Now this part is working and i used the File.Exist before the Dictionary.
The problem when im using the break; is that when its doing the break; its jumping to this part of the code:

<pre class="prettyprint button5.Enabled = true;

myTrackPanelss1.trackBar1.Maximum = counter;
myTrackPanelss1.trackBar1.Value = 0;
setpicture(0);
label5.Visible = false;[/code]
Its going inside the function setpicture and should display in the pictureBox the first file name wich is 00001.bmp
But in setpicture im getting now exception say the file is in use access denied.
I thought the problem was that im using if(File.Exist....
So i changed it to Dictionary but still same exception.

This is the setpicture function:

<pre class="prettyprint private void setpicture(int indx)
{
if (_fi == null)
{
pictureBox1.Image = Lightnings_Extractor.Properties.Resources.Weather_Michmoret;
button5.Enabled = false;
label5.Visible = true;
}
else
{
if (indx >= 0 && indx <= myTrackPanelss1.trackBar1.Maximum && _fi.Length > indx)
{
try
{
label19.ForeColor = Color.Red;
fileToolStripMenuItem.Enabled = true;
label19.Visible = false;
label20.Visible = false;
label14.Visible = true;
label15.Visible = true;
label8.Visible = true;
label9.Visible = true;
// try use a bool flag
myTrackPanelss1.trackBar1.Enabled = true;
using (FileStream fs = new FileStream(_fi[indx].FullName, FileMode.Open))
{
this.label8.Visible = true;
this.label9.Visible = true;
this.label9.Text = _fi[indx].Name;
Image img = null;
Bitmap bmp = null;
Image imgOLd = null;

try
{
// label5.Visible = false; // to make that the label5 "process done" in green will wait like 5 seconds and then will disapear ! using a timer !

img = Image.FromStream(fs);
bmp = new Bitmap(img);

imgOLd = this.pictureBox1.Image;
this.pictureBox1.Image = bmp;
if (imgOLd != null)
imgOLd.Dispose();

img.Dispose();
img = null;
}
catch
{
if (img != null)
img.Dispose();
if (bmp != null)
bmp.Dispose();
if (imgOLd != null)
imgOLd.Dispose();
}
}
}
catch
{
button1.Enabled = false;
label1.Visible = false;
label2.Visible = false;
label3.Visible = false;
label4.Visible = false;
label11.Visible = false;
label12.Visible = false;
checkBox2.Enabled = false;
label19.Visible = true;
label19.ForeColor = Color.Green;
label19.Text = "The Selected Directory Is Access Denied";
label20.Visible = true;
label20.ForeColor = Color.Green;
label20.Text = "Please Set A Different Directory";
fileToolStripMenuItem.Enabled = false;
label14.Visible = false;
label15.Visible = false;
label8.Visible = false;
label9.Visible = false;
myTrackPanelss1.trackBar1.Enabled = false;
timer2.Stop();
return;
}
}
else
{
Image imgOLd = this.pictureBox1.Image;
//this.pictureBox1.Image = null;

if (imgOLd != null)
{
imgOLd.Dispose();
imgOLd = null;
}

Application.DoEvents();
}
}
}[/code]
The exeption in the setpicture function is on the line:

<pre class="prettyprint using (FileStream fs = new FileStream(_fi[indx].FullName, FileMode.Open))[/code]
When its getting to this line its jumping to the catch part and throw:
The process cannot access the file D:New folder (7)MVI_3041.MOV_Automatic00001.bmp because it is being used by another process.
The full exception message:

<pre class="prettyprint System.IO.IOException was caught
Message=The process cannot access the file D:New folder (7)MVI_3041.MOV_Automatic00001.bmp because it is being used by another process.
Source=mscorlib
StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode)
at Extracting_Frames.Form1.setpicture(Int32 indx) in D:C-SharpExtracting_FramesExtracting_FramesExtracting_FramesForm1.cs:line 508
InnerException:
[/code]
So im not sure hwo to fix it. Tried to use Dictionary instead of File.Exist but its not helping.

Thanks.

<hr class="sig danieli

View the full article
 
Back
Top