无法访问StreamReader文件

时间:2017-04-05 19:31:10

标签: c#

无法访问该文件,因为该文件正由另一个进程使用。我已经尝试了一切,但似乎没有任何工作,错误说问题在第61行。我甚至可以找到该文件,我搜索了所有的互联网,无法找到任何帮助。

public class CalendarEntries : List<ICalendarEntry>
 {
    string calendarEntriesFile;
    public bool Load(string calendarEntriesFile)
    {
        bool status = true;
        this.calendarEntriesFile = calendarEntriesFile; //store path for future use

        if (!File.Exists(calendarEntriesFile))
        {
            status = false;
            File.Create(calendarEntriesFile); //create the file as it do not exists
        }
        else
            FillList(); // File exists! :) then what are we waiting for lets read it .  .

        return status;
    }

    private void FillList()
    {
        try
        {
            StreamReader rd = new StreamReader(calendarEntriesFile);
            string line = rd.ReadLine();
            while (line != null)// read line by line in file all apointments
            {
                string[] data = line.Split('#');
                Row row = new Row();
                row.Adopt(data);//create a row of current line i.e appointment
                this.Add(row); // add it to the current list for display
                line = rd.ReadLine();
            }

            rd.Close();
        }
        catch
        {
            MessageBox.Show("Error Occured while reading the appointments File!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    public bool Save(string[] data)
    {
        try
        {
            string line = null;
            for (int i = 0; i < data.Length; i++)
            {
                line += data[i] + "#";
            }
            line = line.Remove(line.Length - 1);

            StreamWriter writer = new StreamWriter(calendarEntriesFile, true);
            writer.WriteLine(line);
            writer.Close();
            return true;
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString());
            return false;
        }
    }

3 个答案:

答案 0 :(得分:0)

您需要丢弃streamreader。尝试使用

using (StreamReader rd = new StreamReader(calendarEntriesFile))
{
        string line = rd.ReadLine();
        while (line != null)// read line by line in file all apointments
        {
            string[] data = line.Split('#');
            Row row = new Row();
            row.Adopt(data);//create a row of current line i.e appointment
            this.Add(row); // add it to the current list for display
            line = rd.ReadLine();
        }
}

这将实际关闭并处理流读取器。你永远不会丢弃它,所以仍然可以参考那个读者。

答案 1 :(得分:0)

CNuts的答案应该解决问题。尝试将每个流包装成一个using语句以及CNuts建议的内容。

如果它不起作用,那么你必须逐步调试你的代码在运行时跟踪所有变量值。

什么时候崩溃?您的代码创建文件或现有文件后?这将定义解决方案。

答案 2 :(得分:0)

<强>被修改

如果你的错误是“文件因为被另一个进程使用而无法访问”,你需要在创建后关闭。这是因为你要将数据附加到文件,而不是替换文件的全部内容。

另外,基于@AgapwIesu答案,您需要同时处理StreamReader和StreamWriter。这是没有文件访问错误的最终代码:

public bool Load(string calendarEntriesFile)
{
    bool status = true;
    this.calendarEntriesFile = calendarEntriesFile; //store path for future use

    if (!File.Exists(calendarEntriesFile))
    {
        status = false;
        File.Create(calendarEntriesFile).Close(); //create the file as it do not exists
    }
    else
        FillList(); // File exists! :) then what are we waiting for lets read it .  .

    return status;
}

private void FillList()
{
    try
    {
        using (StreamReader rd = new StreamReader(calendarEntriesFile))
        {
            string line = rd.ReadLine();
            while (line != null)// read line by line in file all apointments
            {
                string[] data = line.Split('#');
                Row row = new Row();
                row.Adopt(data);//create a row of current line i.e appointment
                this.Add(row); // add it to the current list for display
                line = rd.ReadLine();
            }

            rd.Close();
        }
    }
    catch
    {
        MessageBox.Show("Error Occured while reading the appointments File!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

public bool Save(string[] data)
{
    try
    {
        string line = null;
        for (int i = 0; i < data.Length; i++)
        {
            line += data[i] + "#";
        }
        line = line.Remove(line.Length - 1);

        using (StreamWriter writer = new StreamWriter(calendarEntriesFile, true))
        {
            writer.WriteLine(line);
            writer.Close();
        }
        return true;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
        return false;
    }
}

我测试了一些虚假数据并解决了文件访问问题。