Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Intermittent file access

Status
Not open for further replies.

euntair

Programmer
Sep 13, 2007
53
US
Thank you in advance. I have intermittent access to a .xml file in App_Data. The file is only used in a portion of the program but there are times when it throws a "...because it is being used by another process." error. I've included everything I could find that is supposed to close the file when it is no longer needed. Does anybody have any ideas how to change the code below that releases the file consistently?

public class XmlDocument : System.Xml.XmlDocument {
private WebProjectInfo wpi;

public override void Save(string RelativePath) {
FileStream fs = null;
try {
this.wpi = new WebProjectInfo();
if (this.wpi.FileExists(RelativePath)) {
using (fs = this.wpi.GetFileStream(RelativePath)) {
base.Save(fs);
};
} else {
throw new Exception(RelativePath + " is missing");
}
} catch (Exception ex) {
throw ex;
} finally {
fs.Close();
fs.Dispose();
fs = null;
System.GC.Collect();
}
}
}
 
Code:
if (wpi.FileExists(RelativePath) == false)
{
   throw new Exception(RelativePath + " is missing");
}
using (fs = this.wpi.GetFileStream(RelativePath)) 
{
   base.Save(fs);
}
if you still get the error, the problem lies somewhere else.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thanks Jason. I should have included how I am opening the filestream. Maybe I am doing something wrong in this:
public class WebProjectInfo {

// constructors
public WebProjectInfo() { }

// value methods
public bool FileExists(string RelativePath) {
try{
FileInfo fi = new FileInfo(this.GetFilePath(RelativePath));
return fi.Exists;
} catch (Exception ex) {
throw ex;
}
}

public FileStream GetFileStream(string RelativePath) {
try {
return this.FileExists(RelativePath) == true ?
new FileStream(GetFilePath(RelativePath), FileMode.Open, FileAccess.ReadWrite, FileShare.None) : null;
} catch (Exception ex) {
throw ex;
}
}

public string GetFilePath(string RelativePath) {
return HttpContext.Current.Server.MapPath(RelativePath);
}

}
 
stop doing this
Code:
catch (Exception ex) {
      throw ex;
    }
it provides no value, if anything you are loosing detail about what went wrong.
it also makes the code verbose.

also, if the file resides in the App_Data directory can you assume the file exist? is there ever a time the file would not exist? For example, can a user delete the file? If not you can assume the file does exist and is accessible.

some other things to consider.
If you are not writing to the file, open with read-only access. this way you are not locking the file.
You must dispose of the stream returned by GetFileStream(string). otherwise the file will remain open and locked.
the methods you have here do not seem to provide any real value. nothing is abstracted or encapsulated.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
This code is to display what I am doing to remove the filestream from memory. There is other methods that handle the file not existing, etc.

I did have some luck by placing the instance of the filestream in the .Save method. Since the .Save method does need to store data, I can't use read only. The .Save method below now seems to work every other run.

public override void Save(string RelativePath) {
FileStream fs = null;
try {
this.wpi = new WebProjectInfo();
if (this.wpi.FileExists(RelativePath) == true) {
using (fs = this.wpi.FileExists(RelativePath) == true ?
new FileStream(this.wpi.GetFilePath(RelativePath), FileMode.Open, FileAccess.ReadWrite, FileShare.None) :
null) {
base.Save(fs);
};
} else {
throw new Exception(RelativePath + " is missing");
}
} catch (Exception ex) {
// replacing later...
// throw ex;
} finally {
fs.Close();
fs.Dispose();
fs = null;
System.GC.Collect();
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top