Hi Jon,
here is a relatively short and complete code i have added comments to where
the error occurs in uppercase letters :
class Class1
{
private const string FILE_NAME = "MyInputfile.txt";
private const string FILE_NAME2 = "MyOutputfile.txt";
/// <summary>
/// The main entry point for the application.
/// </summary>
///
[STAThread]
static void Main(string[] args)
{
// if (File.Exists(FILE_NAME))
// {
// Console.WriteLine("{0} already exists.", FILE_NAME);
// return;
// }
StreamReader sr = new StreamReader(FILE_NAME);
string strtext;
//TO GET THE RUNTIME ERROR UNCOMMENT THE //** CODE LINE AND COMMENT THE
LINE BELOW IT
//**StreamWriter sw = new StreamWriter(FILE_NAME2);//IF THIS STREAM IS
FULLY CREATED HERE THEN I GET A RUNTIME ERROR!
//WITH REGARDS TO.....
StreamWriter sw = null;//IF THIS STREAM DECLARED HAS NOT BEEN ASSINGED TO
A NEW OBJECT YET OR HAS BEEN ASSINGED NULL
//can define and assign my string immediately on the line below
strtext = sr.ReadToEnd();
//--------------OR---------
// if ((strtext = sr.ReadToEnd()) != null)
// {
// Console.WriteLine("stream is not null...\nhence Stream has been read ");
// }
//if file exists open and append text to it
if (File.Exists(FILE_NAME2))
{
sw = File.AppendText(FILE_NAME2);// add a \n for new appended text
//display strtext in console
Console.WriteLine(strtext);
//give timestamp
// sw.Write(DateTime.Now);
//Write to file
// sw.Write("......");//write or writelineis the key
sw.WriteLine(strtext); //find how to add a "\n" in notepad..done
automatically nice!!!!
//write out all in underlying stream
sw.Flush();//test it 2morrow...checked already check it tommorow
}
else
{
//assign Stream sw to a new object stream
sw = new StreamWriter(FILE_NAME2);
//give timestamp
// sw.Write(DateTime.Now);
//Write to file
// sw.Write("......");//write or writelineis the key
//Write to file
sw.WriteLine(strtext);
}
//close stream reader and writer
sr.Close();
sw.Close();
hi jon i found the problem with the //** code line ...its because i had
acreated and assigned my StreamWriter sw to a new object ...and yet within
my if statement i try to assign it to a File.Append() method.Silly Me!!!
thanks for your help i really appreciate it. thanks hope we come across each
other on the postings again! cheers.
"Jon Skeet [C# MVP]" wrote:
ShadowOfTheBeast <Sh**************@discussions.microsoft.com> wrote: thanks a lot Jon it works flawlessly now you were right..i am still not very
well versed on streamreaders and writers they seem alittle quirky to me as
this refined code demonstrates in terms of where in the code you declare your
Stream Readers and Streamwriter objects...pay particular attention to the
lines with Capital letters comments...
--------other codes--------------
StreamReader sr = new StreamReader( httpResponse.GetResponseStream() );
string strtext;
//StreamWriter fileWriter = new StreamWriter("OutputTestfile.txt");//THIS
CAUSES AN ISSUE HERE AS REGARDS TO...
//Console.WriteLine(strtext = sr.ReadToEnd());
//write to file
StreamWriter fileWriter = new
StreamWriter("OutputTestfile.txt");//...PLACING IT HERE,WORKS HERE FINE.
//write to the stream object...essentially the outputfile
fileWriter.Write(strtext);
//close stream objects
fileWriter.Close();
sr.Close();
}
That seems very odd - I can't see any reason for that to cause any
difference. Do you have a complete program which always lets you
reproduce the problem (and which always works again when you *just*
move a line of code)?
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too