Reading from a file, what am i doing wrong here ?

C

Claire

I can't see what Ive done wrong with the following, would someone help
please.
I'm trying to read rich text from a file. Ive tested the file using a hex
editor and the contents have been written correctly. When i read back the
file, the result is returned as the file name and not the file contents!!
_BodyFilename is defined by Path.GetTempFileName()
thanks :)
(visual studio 2005 on XP pro)


string result = "";
if (File.Exists(_BodyFilename))
{
StreamReader sr = new StreamReader(_BodyFilename);
try
{
result = sr.ReadToEnd();
}
finally
{
sr.Close();
}
}
return result;
 
G

Guest

Path.GetTempFileName() return a temproray file name with zero sies.
that's why u are not getting any output
 
C

Claire

The file has data in it which was written "earlier". I just generated the
temp file using gettempfilename.
As i said, I tested the file using a hex editor and it contained the correct
contents.
thank you anyway :)
 
J

John Duval

I can't see what Ive done wrong with the following, would someone help
please.
I'm trying to read rich text from a file. Ive tested the file using a hex
editor and the contents have been written correctly. When i read back the
file, the result is returned as the file name and not the file contents!!
_BodyFilename is defined by Path.GetTempFileName()
thanks :)
(visual studio 2005 on XP pro)

string result = "";
if (File.Exists(_BodyFilename))
{
StreamReader sr = new StreamReader(_BodyFilename);
try
{
result = sr.ReadToEnd();}

finally
{
sr.Close();}
}

return result;

Hi Claire,
For what it's worth, I created an RTF using wordpad, and ran your code
and I got the contents of the file just fine. Perhaps you could post
the code that generates the file?

By the way I replaced _BodyFilename with a constant that points to the
file I generated, like so:
string _BodyFilename = @"c:\foo.rtf";

Not sure it matters, but I'm also running VS.NET 2005 under XP Pro.
John
 
G

Guest

every time u write Path.GetTemFilename() generates a new temporary file.
send me ur full code .
 
G

Guest

you should use Path.GetFilename(Filepath)
instead of GetTempFilename
baoth are different function.
GetTempFilename does not take any argument and generate a temproray file
..tmp extension
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top