2011-08-15 23 views
3

我已經創建了一個程序,它能夠通過datagridview修改.exe.config文件(應用程序配置)的內容,其中鍵值對顯示在datagridview的。問題是,我有一個保存設置,它用用戶輸入的新值替換舊值。它會保存,下次我打開它時,文件將被覆蓋,但當我嘗試加載配置文件以更新用戶設置已更改爲的時候,我得到'另一個進程使用的文件'錯誤。如何解決'另一個進程正在使用文件'錯誤

這是代碼:

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(); 
    } 

在下SaveAppSettings的loadconfigfile功能會出現錯誤。它告訴我它不能訪問該文件,因爲該文件被另一個進程使用。在我可以再次打開文件並將其顯示給用戶之前,我需要做些什麼?

非常感謝,

Tf.rz

+0

可能出現[正在被另一個進程使用的文件的重複。原因和解決方案?](http://stackoverflow.com/questions/2861713/file-being-used-by-another-process-reason-and-solution) –

回答

5

這個怎麼樣在loadconfigfile結束()

fIn.Close();

你基本上需要關閉你完成後打開的任何流。

+0

感謝您提示的答案,它不會更新立即,但編輯是一次性的事情,如果用戶希望再次編輯,該文件將不再使用,並加載正確。 –

5

你應該wrapp訪問在using語句流所以它會調用一個Dispose()方法,關閉流爲

using(fIn = new FileStream(configfile, FileMode.Open, FileAccess.ReadWrite)) 
{ 
    // working with file stream 
} 

你PS:通過將空Catch(Exception){}塊。在一些地方,你隱藏的潛在異常或甚至用堆棧跟蹤復位doint throw ex;

+0

謝謝你的回答!如果將來需要,我可能會改用這種方法。並感謝提示,我傾向於那些馬上。乾杯! –

3

您正在編輯它的過程,因爲您在編輯它之後沒有關閉該文件。

您需要先關閉所有流然後再次讀取它。

fIn.Close(); 
sr.Close(); 
sw.Close(); 
+0

謝謝你的回答,但泰盛擊敗你。 ^^ –

相關問題