473,810 Members | 3,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Streamreader/Streamwriter

Using vb.net, I am using Streamreader to read a text file and searching for a
line to delete, the I close the file and open it as a streamwriter so I can
put the new file back to disk without the line that I deleted. Can someone
tell me how I can do this without having to close the file first before
switching to streamwriter, because closing the file allows someone else to
grab the file before I put the new information back. Thanks if any one can
help me!

Joe
Jul 21 '05 #1
2 2436
Try using a temp file, something like this: (sorry no VB)
string path = @"c:\somefile.t xt";
string tempPath = path + ".tmp";
string line = null;
StreamReader reader = null;
StreamWriter writer = null;
try
{
reader = new StreamReader(pa th);
writer = new StreamWriter(te mpPath);

while((line = reader.ReadLine ()) != null)
{
if(line != "useless information")
{
writer.WriteLin e(line);
}
}
}
catch
{
return;
}
finally
{
if(reader != null)
reader.Close();

if(writer != null)
writer.Close();
}

File.Copy(tempP ath, path, true);
File.Delete(tem pPath);

"Joecx" <Jo***@discussi ons.microsoft.c om> wrote in message
news:F3******** *************** ***********@mic rosoft.com...
Using vb.net, I am using Streamreader to read a text file and searching
for a
line to delete, the I close the file and open it as a streamwriter so I
can
put the new file back to disk without the line that I deleted. Can
someone
tell me how I can do this without having to close the file first before
switching to streamwriter, because closing the file allows someone else to
grab the file before I put the new information back. Thanks if any one
can
help me!

Joe

Jul 21 '05 #2
On Thu, 21 Apr 2005 11:17:04 -0700, "Joecx"
<Jo***@discussi ons.microsoft.c om> wrote:
Using vb.net, I am using Streamreader to read a text file and searching for a
line to delete, the I close the file and open it as a streamwriter so I can
put the new file back to disk without the line that I deleted. Can someone
tell me how I can do this without having to close the file first before
switching to streamwriter, because closing the file allows someone else to
grab the file before I put the new information back. Thanks if any one can
help me!

Joe


I have done something similar in C#. Basically I start by renaming
the original copy to a backup version, then I open both the backup
version and a new file with the original name, reading from the backup
and writing to the new file simultaneously. That way the old version
is renamed and the new version is not available for anyone else until
you have finished writing to it.

Below is a simplified version:
(Warning, this has been edited and not compiled so there may be
typos.)

// Backup copy of original.
string fileName = "my_original_fi le.txt";
string backupName = fileName + ".bak";
if (File.Exists(ba ckupName)) { File.Delete(bac kupName); }
File.Move(fileN ame, backupName); // Move(,) renames the file.

// Copy required lines from the backup to the new file
FileInfo originalFile = new FileInfo(backup Name);
StreamReader inStream = originalFile.Op enText();
StreamWriter outStream = new StreamWriter(fi leName);
string thisLine = inStream.ReadLi ne();
while (thisLine != null) {
if (toBeCopied(thi sLine)) { outStream.Write Line(thisLine); }
thisLine = inStream.ReadLi ne();
}
inStream.Close( );
outStream.Close ();

HTH

rossum
The ultimate truth is that there is no ultimate truth
Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
20022
by: Jimbo | last post by:
After I read or write a file with streamreader and streamwriter and I close it with the Close method, does that automatically let go of the file so that any other process can modify it? In my form, I read from a small configuration file with streamreader and close it with streamreader.Close(). In this same event, when I open it for modification with a streamwriter, it tells me the file's being used by another process. Thanks again.
4
8749
by: Astronomically Confused | last post by:
using System; using System.Collections; using System.IO; using System.Net; using System.Net.Sockets; using System.Threading; class HttpProcessor { private Socket s;
9
4600
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 streamwriter, what you have read into a streamReader? and also can someone explain how they work in simple terms -- The Matrix Insurrection
1
4672
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 MemoryStream(); StreamWriter streamWriter = new StreamWriter(stream, Encoding.ASCII); streamWriter.Write(text);
13
3977
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 well with this below shown code (code placed in a buttonclick event after selecting the file in a normal OpenFileDialog): --------------------------------- System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\1.txt"); string all =...
1
4369
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 (info=streamReader.ReadLine) another time, the program hangs. Most likely since the SMTP server has no other responses to post. Is there a way to make the streamreader time out if there is no other info? BTW, the only code not posted is ConnectSocket...it...
16
2096
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, FileGet gets me the correct content while StreamReader gives me a truncated string. Can somebody advise me why? Should I be using BinaryReader instead of StreamReader? Would BinaryReader work on text files?
11
31740
by: LucaJonny | last post by:
Hi, I've got a problem using StreamReader in VB.NET. I try to read a txt file that contains extended characters and theese are removed from the line that is being read. I've read a lot of articles about ANSI encoding like this http://support.microsoft.com/default.aspx?scid=kb;en-us;889835 but System.Text.Encoding.Default don't work!!
3
5237
by: stumorgan | last post by:
I'm doing some USB communications in C# and am running into a minor annoyance. I'm using the Windows API CreateFile function to get a SafeFileHandle, which I then stuff into a FileStream and from there into StreamReader and StreamWriter objects. The StreamReader is working beautifully, but the StreamWriter isn't. If I convert the StreamWriter.BaseStream back to a FileStream and use its SafeFileHandle in the Windows API WriteFile...
0
9722
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10644
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10379
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4334
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.