473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

File being used by another process - Where?

I get the error message

Expand|Select|Wrap|Line Numbers
  1. System.IO.IOException was unhandled
  2.   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.
  3.  
when I attempt to execute...

Expand|Select|Wrap|Line Numbers
  1. for (r = 2; r <= rows+1; r++)
  2.                 {
  3.                     reader.Read();
  4.  
  5.                     if (roomHolder[p] == 0)
  6.                     {
  7.                         roomHolder[p] = setRemaining(roomName[p]);
  8.                         p++;
  9.                         roomHolder[p] = getRemaining(roomName[p]);
  10.                     }
  11.  
  12.                     oTable.Cell(r, 1).Range.Text = reader["STUNO"].ToString();
  13.                     oTable.Cell(r, 2).Range.Text = reader["FNAME"].ToString() + " " + reader["SNAME"].ToString();
  14.                     oTable.Cell(r, 3).Range.Text = roomHolder[p].ToString();
  15.                     oTable.Cell(r, 4).Range.Text = roomName[p];
  16.  
  17.                     roomHolder[p]--;
  18.                 }
  19.  
  20.                 roomHolder[p] = setRemaining(roomName[p]);
  21.  
  22.                 oTable.Rows[1].Range.Font.Bold = 1;
  23.                 oTable.Rows[1].Range.Font.Italic = 1;
  24.  
  25.                 showList.Visible = true;
  26.             }
  27.             else
  28.             {
  29.                 MessageBox.Show("You do not have enough room space to seat all the students taking this exam. Please select or design more rooms.");
  30.             }
  31.         }
  32.  
  33.         private int getRemaining(string room)
  34.         {
  35.             StreamReader sr = new StreamReader(roomDesPath + "/" + room);
  36.             int rInt = Convert.ToInt32(sr.ReadLine());
  37.             sr.Close();
  38.             return rInt;
  39.         }
  40.  
  41.         private int setRemaining(string room)
  42.         {
  43.             StreamReader sr = new StreamReader(roomDesPath + "/" + room);
  44.  
  45.             sr.ReadLine();
  46.  
  47.             string fileString = "";
  48.  
  49.             while (sr.ReadLine() != null)
  50.             {
  51.                 fileString += sr.ReadLine();
  52.             }
  53.  
  54.             sr.Close();
  55.  
  56.             StreamWriter sw = new StreamWriter(roomDesPath + "/" + room);
  57.             sw.WriteLine("0");
  58.             sw.Write(fileString);
  59.  
  60.             return 0;
  61.         }
I have no idea where the lock on the file is coming from because I close the
Expand|Select|Wrap|Line Numbers
  1. 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
May 1 '11 #1
4 8985
adriancs
122 100+
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.
Expand|Select|Wrap|Line Numbers
  1. string FileName = @"C:\5 by 8_design.eh";
  2. string[] stringArray = null;
  3.  
  4. if (File.Exists(FileName))
  5. {
  6.     stringArray = File.ReadAllLines(FileName);
  7.     foreach (string line in stringArray)
  8.     {
  9.         // Do something here...
  10.     }
  11. }
  12. else
  13. {
  14.     MessageBox.Show("File Not Exists");
  15. }
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.
Expand|Select|Wrap|Line Numbers
  1. string filePath = @"C:\";
  2. string fileName = "5 by 8_design.eh";
  3. string[] stringArray = null; // replace this with your data
  4. if (Directory.Exists(filePath))
  5.     File.WriteAllLines(filePath + fileName, stringArray, Encoding.UTF8);
  6. else
  7.     MessageBox.Show("The directory is not exist");
May 1 '11 #2
I've done that but I still get the same error on the StreamWriter. Here's my updated code

Expand|Select|Wrap|Line Numbers
  1. private int setRemaining(string room)
  2.         {
  3.             string fileName = roomDesPath + "/" + room;
  4.             string[] stringArray = null;
  5.  
  6.             if (File.Exists(fileName))
  7.             {
  8.                 stringArray = File.ReadAllLines(fileName);
  9.             }
  10.  
  11.             StreamWriter sw = new StreamWriter(roomDesPath + "/" + room);
  12.             sw.WriteLine("0");
  13.             foreach (string line in stringArray)
  14.             {
  15.                 sw.WriteLine(line);
  16.             }
  17.  
  18.             return 0;
  19.         }
May 1 '11 #3
adriancs
122 100+
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 "roomDesPath" and "room" contain no syntax error.
Expand|Select|Wrap|Line Numbers
  1. private int setRemaining(string room)
  2. {
  3.     string fileName = roomDesPath + "\\" + room;
  4.     string[] stringArray = null;
  5.  
  6.     if (File.Exists(fileName))
  7.     {
  8.         stringArray = File.ReadAllLines(fileName);
  9.     }
  10.  
  11.     StreamWriter sw = new StreamWriter(roomDesPath + "\\" + room);
  12.     sw.WriteLine("0");
  13.     foreach (string line in stringArray)
  14.     {
  15.         sw.WriteLine(line);
  16.     }
  17.     sw.Close();
  18.  
  19.     return 0;
  20. }
May 1 '11 #4
Oops, I had a StreamWriter/Reader open somewhere that I forgot to close. All sorted now. Thanks for your effort!
May 2 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: hntgzr | last post by:
I am trying to include a function in a .php file on a different server from the main .php files. I am using: include_path=http://www.anotherserver.com/foldername;...
4
by: Mountain Bikn' Guy | last post by:
I am having serious problems with the following IDE bug: Could not write to output file 'x.dll' -- 'The process cannot access the file because it is being used by another process. ' and BUG:...
2
by: sidd | last post by:
hiAll, is there a way in .net/c# to check if a file is in use with another process. so i am trying to do a File.Move(source,destination)..and if the file is in use, this would through an...
1
by: Ponnurangam | last post by:
Hi, I have a Web page that displays an image. The image has three buttons at the bottom. I have to delete the image on clicking of one of the button. But when I try to delete the image in...
3
by: Bob | last post by:
I've been repeatedly annoyed by situations where a process has failed or otherwise something has gone wrong (not from any of my own apps) where I cannot move, rename, or delete a file because...
13
by: George | last post by:
Hi, I am re-writing part of my application using C#. This application starts another process which execute a "legacy" program. This legacy program writes to a log file and before it ends, it...
6
by: Studlyami | last post by:
Is it possible to open a file for reading, while another process has it open for writing to it? One of the problems is that the process that is writing to it is written in C and is running on a...
2
by: Newbie | last post by:
Hi, I have a situation where my application is trying to access a file, that another application may have temporalily open. The error I get is The process cannot access the file...
0
by: Tim Golden | last post by:
aditya shukla wrote: Why on earth are you *specifying* c:\python25 as the directory for this file? It's automatically created in your user-specific temp directory which has specific permissions...
3
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post by:
Hi all I am trying to loop a Textfile. This Textfile is located on a shared drive and it is used by another process. When I try to read it I get an exeption: The process cannot access the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.