Basic C# Question

B

Barry Young

I have the following .cs file: Hope you can read the code

In the Main() I want to pass the PathName and FileName and have the
FileStream object do a read, and return it into a string.

I can get intellisense to recognize Source.ReadSourceFile, but I haven't
figured out away to pass the parameters with this call in the Main():

string Test = Source.ReadSourceFile.Read("C:\\MyFile\\", "TestDir.txt");

Here it is:

using System;
using System.IO;

namespace Source
{

/// <summary>
/// Summary description for Class1.
/// </summary>

public class ReadSourceFile
{
public string Read(string PathName, string FileName)
{
System.IO.FileStream file = new System.IO.FileStream(PathName + FileName,
System.IO.FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(file);
string s = sr.ReadToEnd();
return s;
}


} }

public class main
{
string Test = Source.ReadSourceFile.Read("C:\\MyFile\\", "TestDir.txt");
}

///End code here

Any help would be appreciated!

Barry
 
G

Guest

hi barry ,
you can do it using

static void Main(string[] args)

The parameter of the Main method is a string array that represents the
command-line arguments. Usually you check for the existence of the arguments
by testing the Length property

if (args.Length == 0)
error "Please enter arguments"
else
access the arguments using args[0],args[1]..and so on

regards
Ansil
Dimensions
Technopark
Trivandrum
(e-mail address removed)
 
B

Barry Young

Hi Ansil,

Keeping the same context as in my sample code, can you give me an idea how I
could change it to make it work?

Thanks!

Barry

Ansil MCAD said:
hi barry ,
you can do it using

static void Main(string[] args)

The parameter of the Main method is a string array that represents the
command-line arguments. Usually you check for the existence of the
arguments
by testing the Length property

if (args.Length == 0)
error "Please enter arguments"
else
access the arguments using args[0],args[1]..and so on

regards
Ansil
Dimensions
Technopark
Trivandrum
(e-mail address removed)

Barry Young said:
I have the following .cs file: Hope you can read the code

In the Main() I want to pass the PathName and FileName and have the
FileStream object do a read, and return it into a string.

I can get intellisense to recognize Source.ReadSourceFile, but I haven't
figured out away to pass the parameters with this call in the Main():

string Test = Source.ReadSourceFile.Read("C:\\MyFile\\", "TestDir.txt");

Here it is:

using System;
using System.IO;

namespace Source
{

/// <summary>
/// Summary description for Class1.
/// </summary>

public class ReadSourceFile
{
public string Read(string PathName, string FileName)
{
System.IO.FileStream file = new System.IO.FileStream(PathName +
FileName,
System.IO.FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(file);
string s = sr.ReadToEnd();
return s;
}


} }

public class main
{
string Test = Source.ReadSourceFile.Read("C:\\MyFile\\", "TestDir.txt");
}

///End code here

Any help would be appreciated!

Barry
 
S

Stefan Simek

Hi,

I don't exactly understand what are you trying to do (I guess it's not
passing command-line parameters as Ansil wrote).

If you just can't get your code to compile, see inline:

Barry Young said:
Hi Ansil,

Keeping the same context as in my sample code, can you give me an idea how
I could change it to make it work?

Thanks!

Barry

Ansil MCAD said:
hi barry ,
you can do it using

static void Main(string[] args)

The parameter of the Main method is a string array that represents the
command-line arguments. Usually you check for the existence of the
arguments
by testing the Length property

if (args.Length == 0)
error "Please enter arguments"
else
access the arguments using args[0],args[1]..and so on

regards
Ansil
Dimensions
Technopark
Trivandrum
(e-mail address removed)

Barry Young said:
I have the following .cs file: Hope you can read the code

In the Main() I want to pass the PathName and FileName and have the
FileStream object do a read, and return it into a string.

I can get intellisense to recognize Source.ReadSourceFile, but I haven't
figured out away to pass the parameters with this call in the Main():

string Test = Source.ReadSourceFile.Read("C:\\MyFile\\", "TestDir.txt");

Here it is:

using System;
using System.IO;

namespace Source
{

/// <summary>
/// Summary description for Class1.
/// </summary>

public class ReadSourceFile
{
public string Read(string PathName, string FileName)

Replace with: public static string Read(string PathName, string FileName)

If the function is not static, you must first instantiate an object to and
call methods on it.

Where's the Main() method?

static void Main()
{

I hope the rest should work OK Here is a complete code that works for me:

using System;
using System.IO;

namespace Source
{

/// <summary>
/// Summary description for Class1.
/// </summary>

public class ReadSourceFile
{
public static string Read(string pathName, string fileName)
{
using (StreamReader sr = new StreamReader(Path.Combine(pathName,
fileName)))
return sr.ReadToEnd();
}
}
}

public class main
{
static void Main()
{
string test = Source.ReadSourceFile.Read("C:\\MyFile\\",
"TestDir.txt");
}
}

Please note that it's always recommended to dispose objects that implement
the IDisposable interface. Also it's not required to create a FileStream and
then a StreamReader from it.

HTH,
Stefan
 
G

Guest

hi
i got your problem now

the parameters you pass contains slashes
so use it like this

Source.ReadSourceFile.Read(@"C:\\MyFile\\", "TestDir.txt");

and make your Read method of the calss ReadSourceFile as static

public static string Read(string PathName, string FileName)

Ansil
Dimensions
Technopark
Trivandrum
(e-mail address removed)




Barry Young said:
Hi Ansil,

Keeping the same context as in my sample code, can you give me an idea how I
could change it to make it work?

Thanks!

Barry

Ansil MCAD said:
hi barry ,
you can do it using

static void Main(string[] args)

The parameter of the Main method is a string array that represents the
command-line arguments. Usually you check for the existence of the
arguments
by testing the Length property

if (args.Length == 0)
error "Please enter arguments"
else
access the arguments using args[0],args[1]..and so on

regards
Ansil
Dimensions
Technopark
Trivandrum
(e-mail address removed)

Barry Young said:
I have the following .cs file: Hope you can read the code

In the Main() I want to pass the PathName and FileName and have the
FileStream object do a read, and return it into a string.

I can get intellisense to recognize Source.ReadSourceFile, but I haven't
figured out away to pass the parameters with this call in the Main():

string Test = Source.ReadSourceFile.Read("C:\\MyFile\\", "TestDir.txt");

Here it is:

using System;
using System.IO;

namespace Source
{

/// <summary>
/// Summary description for Class1.
/// </summary>

public class ReadSourceFile
{
public string Read(string PathName, string FileName)
{
System.IO.FileStream file = new System.IO.FileStream(PathName +
FileName,
System.IO.FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(file);
string s = sr.ReadToEnd();
return s;
}


} }

public class main
{
string Test = Source.ReadSourceFile.Read("C:\\MyFile\\", "TestDir.txt");
}

///End code here

Any help would be appreciated!

Barry
 
P

Philip Rieck

You can either alter your main to create a "ReadSourceFile" object, then
call "Read' on it, or make "Read" static. The way you have it, you're
trying to call a method on a type, but the method can only be called on an
instance of that type

so for #1 change main (to call the method on an instance):
public class main
{
ReadSourceFile r = new Source.ReadSourceFile();
string Test = r.Read("C:\\MyFile\\", "TestDir.txt");
}

....
or for #2 (to make the method defined on the type, not on instance) change
ReadSourceFile to
public class ReadSourceFile
{
public static string Read(string PathName, string FileName)
{
System.IO.FileStream file = new System.IO.FileStream(PathName + FileName,
System.IO.FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(file);
string s = sr.ReadToEnd();
return s;
}
 

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