How to fix 'file is in use by another process' error
I've created a program that is able to modify the contents of a .exe.config file (app config) through a datagridview with key value pairs displayed on the datagridview. The problem is, I have a save setting, which replaces the old value with a new value typed by the user. It saves, and next time I open it the file will be overwritten, but I get a 'file is used by another process' error when i try to load the config file to update the user on what the setting has been changed to.
This is the code:
private XmlDocument m_XmlDoc;
private FileStream fIn;
private StreamReader sr;
private StreamWriter sw;
private OrderedDictionary m_Settings;
private void ProgramConfig_Load(object sender, EventArgs e)
{
try
{
loadconfigfile(GatewayConfiguration.Properties.Settings.Default.Config);
BindingList<KeyValueType> list = new BindingList<KeyValueType>();
for (index = 0; index < m_Settings.Count; index++)
{
list.Add(new KeyValueType(keys[index], values[index].ToString()));
}
var source = new BindingSource();
source.DataSource = list;
dataGridView1.DataSource = source;
}
catch (Exception ex)
{
textBox1.Text = ex.Message;
}
}
public void loadconfigfile(string configfile)
{
if (File.Exists(configfile))
{
m_XmlDoc = new XmlDocument();
GatewayConfiguration.Properties.Settings.Default.Config = configfile;
GatewayConfiguration.Properties.Settings.Default.Save();
// Error Occurs here at the fIn, telling me that the file is currently in use and cannot be accessed.
fIn = new FileStream(configfile, FileMode.Open, FileAccess.ReadWrite);
sr = new StreamReader(fIn);
sw = new StreamWriter(fIn);
try
{
m_XmlDoc.LoadXml(sr.ReadToEnd());
loadAppSettings();
}
catch (Exception ex)
{
throw ex;
}
}
else
开发者_运维百科 {
throw new FileNotFoundException(configfile + " does not exist.");
}
}
private void loadAppSettings()
{
m_Settings = new OrderedDictionary();
XmlNodeList nl = m_XmlDoc.GetElementsByTagName("setting");
foreach (XmlNode node in nl)
{
try
{
m_Settings.Add(node.Attributes["name"].Value, node.ChildNodes[0].InnerText);
}
catch (Exception)
{
}
}
}
private void SaveAppSettings_Click(object sender, EventArgs e)
{
// saves
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result = MessageBox.Show("Overwrite the old values with the new values?", "Save Settings?", buttons);
if (result == DialogResult.No)
{
return;
}
int index = 0;
string[] keys = new string[m_Settings.Keys.Count];
m_Settings.Keys.CopyTo(keys, 0);
for (index = 0; index < dataGridView1.Rows.Count; index++)
{
if ((string)dataGridView1[2, index].Value != string.Empty)
{
setAppSetting(keys[index], (string)dataGridView1[2, index].Value);
}
}
// Updates datagrid by loading configfile again
loadconfigfile(GatewayConfiguration.Properties.Settings.Default.Config);
textBox1.Text = "Settings Saved. You may now exit.";
m_savecounter++;
dataGridView1.Update();
dataGridView1.Refresh();
}
The error occurs at the loadconfigfile function under SaveAppSettings. It tells me that it cannot access the file because the file is used by another process. Is there something I need to do before I can open the file again and display it to the user?
Many thanks,
Tf.rz
How about this at the end of loadconfigfile()
fIn.Close();
You essentially need to close any stream you opened once you are done.
You should wrapp an access to stream in using
statement so it will call a Dispose()
method which close a stream for you
using(fIn = new FileStream(configfile, FileMode.Open, FileAccess.ReadWrite))
{
// working with file stream
}
PS: In some places you hide a potential exception by placing empty Catch(Exception){}
block or even rethrow with a stack trace reset doint throw ex;
You are the process that is editing it because you didn't close the file after editing it.
You need to close all your Streams before reading from it again.
fIn.Close();
sr.Close();
sw.Close();
精彩评论