这是代码:
private Xml@R_502_4617@ 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 Xml@R_502_4617@(); 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(); }
该错误发生在SaveAppSettings下的loadconfigfile函数中.它告诉我它无法访问该文件,因为该文件由另一个进程使用.在我再次打开文件并将其显示给用户之前,我还需要做些什么吗?
非常感谢,
Tf.rz
解决方法 在loadconfigfile()结束时怎么样?fIn.Close();
一旦完成,您基本上需要关闭您打开的任何流.
总结以上是内存溢出为你收集整理的c# – 如何修复’文件正在被另一个进程使用’错误全部内容,希望文章能够帮你解决c# – 如何修复’文件正在被另一个进程使用’错误所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)