471,338 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 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 2232
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Astronomically Confused | last post: by
9 posts views Thread by ShadowOfTheBeast | last post: by
13 posts views Thread by mloichate | last post: by
1 post views Thread by Rob T | last post: by
11 posts views Thread by LucaJonny | last post: by
reply views Thread by rosydwin | last post: by

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.