473,396 Members | 1,875 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,396 software developers and data experts.

C#, file system

2
Hi

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
  1. public int lookup(String name)
  2.         {
  3.          try
  4.             {
  5.                 StreamReader SR;
  6.                 SR = File.OpenText("c:\\phone book.txt");
  7.                 String S = "test";
  8.                 while (S != null)
  9.                 {
  10.                     String read = SR.ReadLine();
  11.                     if (read.Substring(0, name.Length).Equals(name))
  12.                     {
  13.                         int number = Convert.ToInt32read.Substring(name.Length);
  14.                         SR.Close();
  15.                         return address;
  16.                     }
  17.                 }
  18.             }
  19.  
  20.             catch (Exception e)
  21.             {
  22.                    }
  23.   -----------------------------------------------------------------------------------------------------------------
  24. public void set (String name, int number){
  25.  
  26.             System.IO.StreamWriter SW = new System.IO.StreamWriter("c:\\Yellow Page.txt",true);
  27.             try
  28.             {
  29.                 String input = (name + number).ToString;
  30.                  SW.WriteLine(input);
  31.                 SW.Close();
  32.  
  33.             }
  34.             catch (IOException e)
  35.             {
  36.  
  37.             }
Thank u inadvanced and sorry for posting 2 threads in a short time :-S
Nov 25 '08 #1
1 1419
IanWright
179 100+
Think about what happens if an Exception is thrown during the lookup method. Try putting a break point on SR.Close() and you should see what I mean.

Now try something along the lines of

Expand|Select|Wrap|Line Numbers
  1. StreamReader streamReader = new StreamReader(...);
  2.  
  3. try
  4. {
  5.      // Read from the streamReader 
  6. }
  7. catch()
  8. {}
  9. finally
  10. {
  11.    streamReader.Close();
  12. }
Nov 25 '08 #2

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

Similar topics

11
by: Skc | last post by:
I have a .txt which has been exported as a .csv from an external source. What i need to do is to import this into SQL2000 (into a table) but I need to do special things on the data: 1. I need to...
3
by: Michael Bøcker-Larsen | last post by:
Hi I'v been stuck on this problem for ages now. I have found that I'm not the only one with this problem, by looking through the different newsgroups. Hope you can help me! I know there is a...
2
by: aallee83 | last post by:
i'm new in asp.net after develop my solution i copied it on the server where I want it to run but something cares... WHAT?!?! thank you in advance File or assembly name System, or one of its...
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
0
by: Filemaxor | last post by:
I have gotten my code to be able to allow people to add new text to a .txt document and able to call up files so the user can see whats in it. The problem i'm having is getting the for loop to work...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.