0

The file can't be accessed because its being used by another process. I have tried everything but nothing seems to work, the error says problem is here in line 61. I can even locate the file and I have searched all the internet and can't find any help.

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;
        }
    }
Simson
  • 3,373
  • 2
  • 24
  • 38
  • 2
    Well yes - so what other process is accessing it? Fundamentally unless you work hard specifying the right file share mode everywhere you *won't* be able to get multiple processes accessing the same file at the same time. It's generally simpler just to make sure that they don't even try. – Jon Skeet Apr 05 '17 at 19:32
  • i dont know what process is accessing it, it was working fine before but i dont kno what happened –  Apr 05 '17 at 19:33
  • how can i fix this?? –  Apr 05 '17 at 19:34
  • Do you mean that there should not be any other process accessing this file? But you are still getting this message? –  Apr 05 '17 at 19:35
  • when i run it it doesnt read the file –  Apr 05 '17 at 19:35
  • what could be the problem –  Apr 05 '17 at 19:36
  • You will need to give more detail. It is kind of hard to help someone debug a problem by looking at only one line of code. –  Apr 05 '17 at 19:36
  • i dont understand whats the problem?? it says the file cant be accessed because its used another processes –  Apr 05 '17 at 19:38
  • 1
    I would not be surprised if it is your own code that keeps the file locked. Do you use that file before this line? Please add more context (code) at your question. – Steve Apr 05 '17 at 19:39
  • i have now added more –  Apr 05 '17 at 19:40
  • help pls im struggliing –  Apr 05 '17 at 19:50
  • Close your streams in finally block since you may get exceptions. Put rd.close() and writer.close() in finally block after catch – Efe Apr 05 '17 at 19:59
  • where? can you post it –  Apr 05 '17 at 20:01
  • For example in FileList function : catch{...} finally{ rd.close();} . The safest way to close streams is in finally block. – Efe Apr 05 '17 at 20:04
  • i dont get what u mean??? –  Apr 05 '17 at 20:06

3 Answers3

0

You need to dispose of the streamreader. Try using

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

That will actually close and dispose of the streamreader. You never dispose of it, so a reference to that reader is still out there.

  • now i've got a different error saying array was outside of boundary –  Apr 05 '17 at 20:05
  • So this solved the problem you had with the file. A StreamReader is not disposed of if you just let it get garbage collected. See @JonSkeet answer http://stackoverflow.com/a/1065196/4843530 –  Apr 05 '17 at 20:08
  • i am not going to debug your entire program for you. Your question is answered. You should mark it so, and post a different question if you have something else you need help with. **But** you should make every effort to debug the rest of your program before you ask for help here. –  Apr 05 '17 at 20:10
  • Date = DateTime.Parse(line[4); –  Apr 05 '17 at 20:17
  • help pls Agapwlesu –  Apr 05 '17 at 20:20
0

CNuts answer ought to solve the problem. Try wrapping each stream into a using statement together with what CNuts suggested.

If it doesnt work despite this then you have to debug your code step by step tracing all the variable values in runtime.

When does it crash? After when your code creates the file or with already existing files too? That will define the solution.

DigheadsFerke
  • 307
  • 1
  • 8
0

Edited

If your error is " file cant be accessed because its being used by another process", You need to close if after creation.It is because you are appending data to file, not replacing the whole contents of file.

Also, based on @AgapwIesu answer, you need to dispose both StreamReader and StreamWriter. This is the final code without file access error:

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

I tested this with some fake data and file access problem solved.

Efe
  • 800
  • 10
  • 32