Deleting file issue

jvi66

New member
Joined
Nov 3, 2003
Messages
3
Everytime that I try to delete a file I get the following error: "file is being used by another process". I checked the permissions and everything looks right. Here is the code, could anybody check what Im doing wrong?

private void SavePicture()
{
//This function will move the picture from a given directory to the
//picture directory where all the pictures will be saved.
//Once the picture has been copied into the directory the database will be updated
//with the path and file name of the saved picture.

//first copy the pictures to the specified directory
Cursor.Current = Cursors.WaitCursor;
bErrorFound = false;
string sNewPicture;
string sDirectory = SchoolDBMain.WorkingDirectory;

//Now check if the Picture Directory Exist, if it doesnt exist create it.
sDirectory = sDirectory + "\\Pictures";
try
{
if (!Directory.Exists(sDirectory))
{
Directory.CreateDirectory(sDirectory);
}
}
catch (Exception e)
{
MessageBox.Show(this,e.Message,SchoolDBMain.rm.GetString("txtLoadPicture"),MessageBoxButtons.OK,MessageBoxIcon.Error);
bErrorFound = true;
goto EndFunction;
}


//If we are here is because the Picture Directory Exist
//Move picture from Original Path to Picture Directory

//Make sure that the Picture hasnt been deleted or moved since we selected the
//Picture
if (!File.Exists(sOriginalPath))
{
MessageBox.Show(this,SchoolDBMain.rm.GetString("txtPictureDeleted"),SchoolDBMain.rm.GetString("txtLoadPicture"),MessageBoxButtons.OK,MessageBoxIcon.Error);
bErrorFound = true;
goto EndFunction;
}

//OK We established that the file Exist so change attributes
try
{
//Set the attribute to Normal
File.SetAttributes(sOriginalPath,FileAttributes.Normal);
}
catch (Exception e)
{
MessageBox.Show(this,e.Message,SchoolDBMain.rm.GetString("txtLoadPicture"),MessageBoxButtons.OK,MessageBoxIcon.Error);
bErrorFound = true;
goto EndFunction;
}
//Get new directory
sNewPicture = sDirectory + "\\" + lStudentKey.ToString() + sOriginalPath.Substring(sOriginalPath.Length - 4, 4);

//Check if the destination picture and original picture are the same
if (sOriginalPath == sNewPicture)
{
MessageBox.Show(this,SchoolDBMain.rm.GetString("txtSamePicture"),SchoolDBMain.rm.GetString("txtLoadPicture"),MessageBoxButtons.OK,MessageBoxIcon.Error);
goto EndFunction;
}


//Check if the Picture already exist in the Picture directory
try
{
if (File.Exists(sNewPicture))
{
//Change attribute to normal
File.SetAttributes(sNewPicture,FileAttributes.Normal);

File.Delete(sNewPicture); }
}
catch (Exception e)
{
MessageBox.Show(this,e.Message,SchoolDBMain.rm.GetString("txtLoadPicture"),MessageBoxButtons.OK,MessageBoxIcon.Error);
bErrorFound = true;
goto EndFunction;
}


//Attributes has been changed so now copy the file to the picture directory
try
{
File.Copy(sOriginalPath,sNewPicture);
File.SetAttributes(sNewPicture,FileAttributes.ReadOnly); //Set the file to read only
File.SetAttributes(sNewPicture,FileAttributes.Hidden); //Set to hidden so nobody see them
// File.SetAttributes(sNewPicture,FileAttributes.Compressed); //Compress file
}
catch (Exception e)
{
MessageBox.Show(this,e.Message,SchoolDBMain.rm.GetString("txtLoadPicture"),MessageBoxButtons.OK,MessageBoxIcon.Error);
bErrorFound = true;
goto EndFunction;
}


//Open the JPG file
FileStream fs = new FileStream(sOriginalPath,System.IO.FileMode.Open,System.IO.FileAccess.Read);
//Read the output in a stream reader
StreamReader sr = new StreamReader(fs);
byte[] byteArray = new byte[fs.Length -1];

try
{
//Declare a byte array to save the content of the file
ASCIIEncoding AE = new ASCIIEncoding();
byteArray = AE.GetBytes(fs.ToString());
}

catch (Exception e)
{
MessageBox.Show(this,e.Message,SchoolDBMain.rm.GetString("txtLoadPicture"),MessageBoxButtons.OK,MessageBoxIcon.Error);
bErrorFound = true;
goto EndFunction;
}

//OK if we are here it means that we almost there...so
//Save the path in the database
Students student = new Students();
string sMsg;
sMsg = student.SavePicture(lStudentKey,byteArray);
if (sMsg != "0")
{
sMsg = "Error Found: " + sMsg;
MessageBox.Show(this,sMsg,SchoolDBMain.rm.GetString("txtLoadPicture"),MessageBoxButtons.OK,MessageBoxIcon.Error);
bErrorFound = true;
goto EndFunction;
}
//Update Array list
for (int i = 0; i < PPath.Length; i++)
{
if (PPath.StudentKey == lStudentKey.ToString())
{
//PPath.Path = sNewPicture;
PPath.Path = sOriginalPath;
break;
}
}

ClearFields();
EndFunction:
Cursor.Current = Cursors.Default;

}
 
Thanks sysop, but Im getting the error on the delete even before I try to open the file stream.

//Check if the Picture already exist in the Picture directory
try
{
if (File.Exists(sNewPicture))
{
//Change attribute to normal
File.SetAttributes(sNewPicture,FileAttributes.Normal);

File.Delete(sNewPicture); }
}
catch (Exception e)
{
MessageBox.Show(this,e.Message,SchoolDBMain.rm.GetString("txtLoadPicture"),MessageBoxButtons.OK,MessageBoxIcon.Error);
bErrorFound = true;
goto EndFunction;
}
 
have you tried using the while statement / with Application.DoEvents(); ? eg:
C#:
try
{
while(File.Exists(sNewPicture))
{
    //Change attribute to normal
    File.SetAttributes(sNewPicture,FileAttributes.Normal);
    Application.DoEvents(); //
    File.Delete(sNewPicture); 
}
}
}
    catch (Exception e)
 
Thank you very much Sysop!!! That fixed the problem. I just needed to give time to the OS to finish changing the attributes. Im new here, should I give points to you?? Thanks again!!!
 
Back
Top