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

I need help on Streamreader and Streamwriter, please

I have this kind of exception:
System.IO.IOException: The process cannot access the file 'E:\asem\Visual Studio 2005\WebSites\WebSite1\App_Data\Books.txt' because it is being used by another process.

and don't know the solution.
Here is the code, somebody help me please, and thank you in advance.

protected void AddBook()
{
string strISBN = tbISBN.Text;
string filePath = Server.MapPath("Books.txt");

FileStream file = new FileStream(filePath, FileMode.Append, FileAccess.Write);

try
{
StreamReader sr = new StreamReader(filePath);
content = sr.ReadToEnd();
sr.Close();

if (content.Contains(strISBN))
{
lblUploadResult.Text = "isbn contained";
}
else
{
StreamWriter sw =new StreamWriter(filePath);
sw.WriteLine(tbTitle.Text + "," + tbISBN + "," + tbAuthor + "," + tbPublisher + "," + tbPrice + ",");
sw.Close();
}
}
catch (Exception) { }
}
Mar 21 '07 #1
2 1883
sani723
117 100+
file.Close();
Mar 21 '07 #2
I'm sorry, that code wasn't the correct one, here is it:

Expand|Select|Wrap|Line Numbers
  1. StreamReader sr = File.OpenText(filePath);
  2.         StreamWriter sw = File.AppendText(filePath);
  3.  
  4.         try
  5.         {
  6.             content = sr.ReadToEnd();
  7.             if (content.Contains(strISBN))
  8.             {
  9.                 lblUploadResult.Text = "isbn contained";
  10.             }
  11.             else
  12.             {           
  13.                 sw.WriteLine(tbTitle.Text + "," + tbISBN + "," + tbAuthor + "," + tbPublisher + "," + tbPrice + ",");
  14.  
  15.             }
  16.         }
  17.         catch (Exception) { }
  18.         sr.Close();

and it shows me the exception: System.IO.IOException: The process cannot access the file 'E:\asem\Visual Studio 2005\WebSites\WebSite1\App_Data\Books.txt' because it is being used by another process.
Mar 21 '07 #3

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

Similar topics

9
by: ShadowOfTheBeast | last post by:
Hi, I have got a major headache understanding streamReader and streamWriter relationship. I know how to use the streamreader and streamwriter independently. but how do you write out using the...
1
by: R.L. | last post by:
See the code below, var 'content ' is suppose to be "Hello!", not "". Who knows why? Thanks ---------------------------------------- string text = "hello!"; MemoryStream stream = new...
13
by: mloichate | last post by:
I must read a very heavy-weight text plain file (usually .txt extension) )and replace a given character with another given character in all text inside the file. My application was working pretty...
1
by: Rob T | last post by:
Hi, I have a simple little program that I'm going to use to access our SMTP server. The below code works perfectly fine, but my question is if I were to try to execute the last line...
16
by: vvenk | last post by:
Hello: When I use either one to read a Text file, I get the same result. The length of the string that the file's content has been written into is the same. However, if the file is binary,...
2
by: Terry Olsen | last post by:
Hoping someone can help me here. I've got this code written, and it works fine for the first connection. But if I connect another client (while the first is still connected), I get connected but...
1
by: jens Jensen | last post by:
Hello , i'm calling a webservice generated with oracle webservice java tools. I'm not able to add a web reference to a .net client the usual way with visual studio 2005. I was therefore...
0
by: =?Utf-8?B?T3JsYW5kbw==?= | last post by:
Hi, I'm troubleshooting an issue in the field where a simple text file that gets read/written over and over again ends up zeroed out somehow (ie it should have XML, but when looked at in a...
0
by: rajana | last post by:
Dear All, We have Ansi file with german characters (Ä / Ø) , We are using Streamreader to read the contents of the file. But Readline() not able to read the German characters. We tried all...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.