加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何修复’文件正在被另一个进程使用’错误

发布时间:2020-12-16 00:17:25 所属栏目:百科 来源:网络整理
导读:我创建了一个程序,它能够通过datagridview修改.exe.config文件(app config)的内容,并在datagridview上显示键值对.问题是,我有一个保存设置,它将旧值替换为用户键入的新值.它保存,下次打开它时,文件将被覆盖,但当我尝试加载配置文件以更新用户设置已更改为时,
我创建了一个程序,它能够通过datagridview修改.exe.config文件(app 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

解决方法

在loadconfigfile()结束时怎么样?

fIn.Close();

一旦完成,您基本上需要关闭您打开的任何流.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读