2011-08-15 15 views
3

私は、ファイルに表示されているキー値のペアを持つdatagridviewを使って.exe.configファイル(app config)の内容を変更できるプログラムを作成しました。データグリッドビュー。問題は、古い値をユーザーが入力した新しい値に置き換える保存設定があることです。保存すると、次回にファイルを上書きすると上書きされますが、設定ファイルを読み込んで設定を変更したユーザーを更新しようとすると、「別のプロセスでファイルが使用されています」というエラーが表示されます。'ファイルが別のプロセスで使用中です'というエラーを修正するには

これはコードである:

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

あなたはそれはあなたが

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

PS用のストリームを閉じるDispose()メソッドを呼び出しますのでusing文でストリーミングするためのアクセスをwrappする必要がありますいくつかの場所では、あなたが潜在的な例外を隠す空Catch(Exception){}ブロックを配置することによって、またはスタックトレースを使って再スローするdoint throw ex;

+0

あなたの答えをありがとう!私はこのアプローチを将来必要に応じて切り替えるかもしれません。ヒントのおかげで、私はすぐにそれらに傾向がある。乾杯! –

3

編集後にファイルを閉じなかったため、編集中のプロセスです。

ストリームをもう一度読む前にすべてのストリームを閉じる必要があります。

+0

あなたの答えをありがとうが、泰成はそれにあなたを打ち負かす。 ^^ –