.NET Framework 2.0 features on VS 2005.

P

Paul Lemelle

I am tyring to use the .NET Framework 2.0 with Visual Studio 2005, but
I cannot get the System.IO / class method File ReadAllText feature to
work. How can I get this to work with VS 2005?

Thanks,
Paul
 
M

Michael Nemtsev

Hello Paul,

What the problem is?
Have u tried MSDN sample http://msdn2.microsoft.com/en-us/library/ms143368.aspx
?

PL> I am tyring to use the .NET Framework 2.0 with Visual Studio 2005,
PL> but I cannot get the System.IO / class method File ReadAllText
PL> feature to work. How can I get this to work with VS 2005?
PL>
PL> Thanks,
PL> Paul
---
WBR, Michael Nemtsev [C# MVP] blog: http://spaces.live.com/laflour
team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo
 
R

RobinS

In VB2005:

'Read in a file and split the lines by CrLf
Dim crlfs() as String = {ControlChars.CrLf}
Dim lines() as String = _
File.ReadAllText("c:\data.txt").Split(crlfs, StringSplitOptions.None)
Dim numOfLines = lines.Length

If you want more help, you will need to tell exactly what's
not working, and post your code.

Robin S.
 
P

Paul Lemelle

I am using C#, and here is the code:

using system;
using system.IO;

namespace FileIO

Class File
{
public static void Main ()
{
string myfile = File.ReadAllText("myfile");
// I do not get the File.ReadAllText - I only get Equals, Main,
// & ReferenceEquals after File.
Console.Writeline(myfile);
}
}

The code fills because the compiler does not know what to do with the
File.ReadAllText method.

Paul
 
R

RobinS

I tried this, and it worked fine. I think your problem is your file name
"myfile". My guess is that it can not find the file. Try a file with a
path, like "C:\Test.txt".

Also be sure you have "using System.IO;" at the top of your class.

And Writeline should be WriteLine. C# is case-sensitive.

Robin S.
-------------------------------------
 
N

Norman Yuan

Naming you class as "File" is a bad choice.

You code will not compile, since you also used "System.IO" name space, which
has a class called "File". You need to qualify the class "File" to
distinguish which namespace it belongs to:

string myfile=System.IO.File.ReadAllText(...) //If the ReadAllText is not
from your custom class

or

string myfile=FileIO.File.ReadAllText(...) //If your "File" class
defines the method with the same name.

I strongly recommend you name your namespace/class wisely.
 
P

Paul Lemelle

hi Norman,

That was it - thanks.

Paul


Naming you class as "File" is a bad choice.

You code will not compile, since you also used "System.IO" name space, which
has a class called "File". You need to qualify the class "File" to
distinguish which namespace it belongs to:

string myfile=System.IO.File.ReadAllText(...) //If the ReadAllText is not
from your custom class

or

string myfile=FileIO.File.ReadAllText(...) //If your "File" class
defines the method with the same name.

I strongly recommend you name your namespace/class wisely.
 
J

Jon Skeet [C# MVP]

Paul Lemelle said:
I am using C#, and here is the code:

using system;
using system.IO;

namespace FileIO

Class File
{
public static void Main ()
{
string myfile = File.ReadAllText("myfile");
// I do not get the File.ReadAllText - I only get Equals, Main,
// & ReferenceEquals after File.
Console.Writeline(myfile);
}
}

That's certainly not the code you had mostly working - it's really
helpful if you cut and paste the *actual* code in, rather than creating
a *similar* program which will fail for other reasons. Problems with
the above, *apart* from the collision of the "File" name:

1) Case of "system"
2) No braces after the namespace declaration
3) Case of class declaration
4) Case of the "l" in Writeline

In general, if you've already got a problem in your code, it makes it
hard for us to tell the problems which are *really* there compared with
typos when retyping the code unless you use cut and paste properly.
 

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