Im trying to write a class to replace strings in a text file but for some reason StreamWriter will not work in my class. Its not throwing any exceptions or anything its just not writing to the text file. Also I used to have a destructor in my class but it was throwing an exception when I tried to close or dispose outFile. -
class TextReplacer
-
{
-
private StreamReader inFile;
-
private StreamWriter outFile;
-
-
public TextReplacer(StreamReader _in, StreamWriter _out)
-
{
-
inFile = _in;
-
outFile = _out;
-
-
outFile.WriteLine("TESTING!!!"); // DOES NOT WORK
-
}
-
-
public void Repace(string find, string replace)
-
{
-
string line = inFile.ReadLine();
-
-
if (line != null)
-
{
-
line = line.Replace(find, replace);
-
-
Console.WriteLine(line);
-
-
outFile.WriteLine(line); // DOES NOT WORK
-
-
Repace(find, replace);
-
}
-
-
return;
-
}
-
}
-
I'm betting this is probably because you still have the file open in read-only mode. A quick way to do what your trying to do could be: -
public void ReplaceInFile(string file, string find, string replace)
-
{
-
string buffer = File.ReadAllText(file);
-
buffer = buffer.Replace(find, replace);
-
File.WriteAllText(file, buffer);
-
}
-
However if you needed to use StreamReader and StreamWriter within a class, then this is probably what your looking for: -
public void Replace(string find, string replace)
-
{
-
string buffer = "";
-
using(FileStream fs = new FileStream(m_Filename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
-
{
-
StreamReader sr = new StreamReader(fs);
-
buffer = sr.ReadToEnd();
-
-
buffer = buffer.Replace(find, replace);
-
-
fs.Position = 0;
-
-
using(StreamWriter sw = new StreamWriter(fs))
-
{
-
sw.Write(buffer);
-
sw.Flush();
-
sw.Close();
-
sr = null;
-
}
-
}
-
}
-
All the best.
Aimee.
1 7931
I'm betting this is probably because you still have the file open in read-only mode. A quick way to do what your trying to do could be: -
public void ReplaceInFile(string file, string find, string replace)
-
{
-
string buffer = File.ReadAllText(file);
-
buffer = buffer.Replace(find, replace);
-
File.WriteAllText(file, buffer);
-
}
-
However if you needed to use StreamReader and StreamWriter within a class, then this is probably what your looking for: -
public void Replace(string find, string replace)
-
{
-
string buffer = "";
-
using(FileStream fs = new FileStream(m_Filename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
-
{
-
StreamReader sr = new StreamReader(fs);
-
buffer = sr.ReadToEnd();
-
-
buffer = buffer.Replace(find, replace);
-
-
fs.Position = 0;
-
-
using(StreamWriter sw = new StreamWriter(fs))
-
{
-
sw.Write(buffer);
-
sw.Flush();
-
sw.Close();
-
sr = null;
-
}
-
}
-
}
-
All the best.
Aimee.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: gatekeeper |
last post by:
Why could it be that i am able to use StreamWriter from a
windows application but not from a web application?
I am getting an unauthorizedaccessexception and the
exception message is "Access to...
|
by: Joshua Campbell |
last post by:
I am working on an application that needs to parse out data that has been
copied into the clipboard. My first questions are how can I detect if the
clipboard is empty, and how can I empty the...
|
by: Don |
last post by:
I have a strange bug popping up every once in a while. I've got a section
of code that executes these statements when working with a streamwriter:
--- Start ---
....
' Close the...
|
by: Anthony Nystrom |
last post by:
I am trying to pass a text stream from streamwriter class to a richtextbox,
or textbox doesn't matter which one. I have the file write version working
fine such as:
x.streamwriter =...
|
by: Don |
last post by:
I'm having problems working with a streamwriter object. After closing the
streamwriter and setting it to Nothing, I try to delete the file it was
writing to, but I always get the following error...
|
by: Karan |
last post by:
I have already imported system.io. Reader object is working fine: Dim sr As
New StreamReader(fstream). Writer object
Dim sw As New StreamWriter(fstream) object shows blue under line with type...
|
by: paul.hine |
last post by:
Hello,
I maintain an application that pulls data from a web service and writes
it to an Excel sheet. Originally, the app used OleDb to write the
Excel. Exports ran fine except that some text...
|
by: MSwanston |
last post by:
Hi
I need some help with saving retreiving data from the cache, and how best to structure my code. FYI am working in VS2005/under .NET2 Framework.
Ok, we have a series of reports that get run via...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |