473,396 Members | 1,916 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.

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 2399
Try using a temp file, something like this: (sorry no VB)
string path = @"c:\somefile.txt";
string tempPath = path + ".tmp";
string line = null;
StreamReader reader = null;
StreamWriter writer = null;
try
{
reader = new StreamReader(path);
writer = new StreamWriter(tempPath);

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

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

File.Copy(tempPath, path, true);
File.Delete(tempPath);

"Joecx" <Jo***@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.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***@discussions.microsoft.com> 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_file.txt";
string backupName = fileName + ".bak";
if (File.Exists(backupName)) { File.Delete(backupName); }
File.Move(fileName, backupName); // Move(,) renames the file.

// Copy required lines from the backup to the new file
FileInfo originalFile = new FileInfo(backupName);
StreamReader inStream = originalFile.OpenText();
StreamWriter outStream = new StreamWriter(fileName);
string thisLine = inStream.ReadLine();
while (thisLine != null) {
if (toBeCopied(thisLine)) { outStream.WriteLine(thisLine); }
thisLine = inStream.ReadLine();
}
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
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...
4
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
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,...
11
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...
3
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...
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
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:
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
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
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...

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.