File being used by another process - Where?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Dransfield
    New Member
    • Jan 2011
    • 46

    File being used by another process - Where?

    I get the error message

    Code:
    System.IO.IOException was unhandled
      Message=The process cannot access the file 'C:\Users\Alex\Desktop\ExamsHelper\ExamsHelper\bin\x86\Debug\roomdesigns\5 by 8_design.eh' because it is being used by another process.
    when I attempt to execute...

    Code:
    for (r = 2; r <= rows+1; r++)
                    {
                        reader.Read();
    
                        if (roomHolder[p] == 0)
                        {
                            roomHolder[p] = setRemaining(roomName[p]);
                            p++;
                            roomHolder[p] = getRemaining(roomName[p]);
                        }
    
                        oTable.Cell(r, 1).Range.Text = reader["STUNO"].ToString();
                        oTable.Cell(r, 2).Range.Text = reader["FNAME"].ToString() + " " + reader["SNAME"].ToString();
                        oTable.Cell(r, 3).Range.Text = roomHolder[p].ToString();
                        oTable.Cell(r, 4).Range.Text = roomName[p];
    
                        roomHolder[p]--;
                    }
    
                    roomHolder[p] = setRemaining(roomName[p]);
    
                    oTable.Rows[1].Range.Font.Bold = 1;
                    oTable.Rows[1].Range.Font.Italic = 1;
    
                    showList.Visible = true;
                }
                else
                {
                    MessageBox.Show("You do not have enough room space to seat all the students taking this exam. Please select or design more rooms.");
                }
            }
    
            private int getRemaining(string room)
            {
                StreamReader sr = new StreamReader(roomDesPath + "/" + room);
                int rInt = Convert.ToInt32(sr.ReadLine());
                sr.Close();
                return rInt;
            }
    
            private int setRemaining(string room)
            {
                StreamReader sr = new StreamReader(roomDesPath + "/" + room);
    
                sr.ReadLine();
    
                string fileString = "";
    
                while (sr.ReadLine() != null)
                {
                    fileString += sr.ReadLine();
                }
    
                sr.Close();
    
                StreamWriter sw = new StreamWriter(roomDesPath + "/" + room);
                sw.WriteLine("0");
                sw.Write(fileString);
    
                return 0;
            }
    I have no idea where the lock on the file is coming from because I close the
    Code:
    StreamReader
    before I open the writer. I'm really tired and I'm just about to sleep, so hopefully someone may be able to identify the problem when I wake up, otherwise I'll sink a few more hours into it :D
  • adriancs
    New Member
    • Apr 2011
    • 122

    #2
    is this "5 by 8_design.eh" a text file?
    if yes, you may try this to read the content of file.
    The following code will close the file automatically after reading.
    Code:
    string FileName = @"C:\5 by 8_design.eh";
    string[] stringArray = null;
    
    if (File.Exists(FileName))
    {
        stringArray = File.ReadAllLines(FileName);
        foreach (string line in stringArray)
        {
            // Do something here...
        }
    }
    else
    {
        MessageBox.Show("File Not Exists");
    }
    The above code will read all lines and close the file automatically.

    The below code will write all lines into the file and close the file automatically.
    Code:
    string filePath = @"C:\";
    string fileName = "5 by 8_design.eh";
    string[] stringArray = null; // replace this with your data
    if (Directory.Exists(filePath))
        File.WriteAllLines(filePath + fileName, stringArray, Encoding.UTF8);
    else
        MessageBox.Show("The directory is not exist");

    Comment

    • Alex Dransfield
      New Member
      • Jan 2011
      • 46

      #3
      I've done that but I still get the same error on the StreamWriter. Here's my updated code

      Code:
      private int setRemaining(string room)
              {
                  string fileName = roomDesPath + "/" + room;
                  string[] stringArray = null;
      
                  if (File.Exists(fileName))
                  {
                      stringArray = File.ReadAllLines(fileName);
                  }
      
                  StreamWriter sw = new StreamWriter(roomDesPath + "/" + room);
                  sw.WriteLine("0");
                  foreach (string line in stringArray)
                  {
                      sw.WriteLine(line);
                  }
      
                  return 0;
              }

      Comment

      • adriancs
        New Member
        • Apr 2011
        • 122

        #4
        Hi, I have made some of the modification of your code, you may try this. Before that, you have to make sure that the variable of "roomDesPat h" and "room" contain no syntax error.
        Code:
        private int setRemaining(string room)
        {
            string fileName = roomDesPath + "\\" + room;
            string[] stringArray = null;
        
            if (File.Exists(fileName))
            {
                stringArray = File.ReadAllLines(fileName);
            }
                    
            StreamWriter sw = new StreamWriter(roomDesPath + "\\" + room);
            sw.WriteLine("0");
            foreach (string line in stringArray)
            {
                sw.WriteLine(line);
            }
            sw.Close();
        
            return 0;
        }

        Comment

        • Alex Dransfield
          New Member
          • Jan 2011
          • 46

          #5
          Oops, I had a StreamWriter/Reader open somewhere that I forgot to close. All sorted now. Thanks for your effort!

          Comment

          Working...