Sorry I have to post another thread be4 leaving to the uni!
I have a problem with file system. I have a phonebook service with has 2 methods:
Set: adds new items to phone book
lookup: looking up a name to find its tel.number.
If first i use "set" and then "lookup" it's ok! But when first i look up a name and when i can't find it , i try to add it to phone book , i got this error: Access to this fileis not possible, becaz this file is in used by another process.
here is my code:
Expand|Select|Wrap|Line Numbers
- public int lookup(String name)
- {
- try
- {
- StreamReader SR;
- SR = File.OpenText("c:\\phone book.txt");
- String S = "test";
- while (S != null)
- {
- String read = SR.ReadLine();
- if (read.Substring(0, name.Length).Equals(name))
- {
- int number = Convert.ToInt32read.Substring(name.Length);
- SR.Close();
- return address;
- }
- }
- }
- catch (Exception e)
- {
- }
- -----------------------------------------------------------------------------------------------------------------
- public void set (String name, int number){
- System.IO.StreamWriter SW = new System.IO.StreamWriter("c:\\Yellow Page.txt",true);
- try
- {
- String input = (name + number).ToString;
- SW.WriteLine(input);
- SW.Close();
- }
- catch (IOException e)
- {
- }