creating a file

S

sahel

Hi all;
I wrote a program that it must create a file & it doesn’t has error
but at its runtime it has this error

Unhandled exception :system. Index out of range exception :index was
outside the bounds of the array

using System;
using System.IO;
namespace ConsoleApplication85
{
class Program
{
static void Main(string[] args)
{
StreamWriter myfile = File.CreateText(args[0]);
myfile.WriteLine("*********");
myfile.Close();
Console.WriteLine("****");


}
}
}
 
F

Futu Ranon

static void Main(string[] args)
{
StreamWriter myfile = File.CreateText(args[0]);

Enter the debugger and take a look at args. Are you passing an argument to
the program?
 
S

sahel

static void Main(string[] args)
        {
             StreamWriter myfile = File.CreateText(args[0]);

Enter the debugger and take a look at args. Are you passing an argument to  
the program?

:blush:
i don't know how to do that & i dont know anything a bout args ( i am
new at c# ) [ :-( ]
 
M

mick

Hi all;
I wrote a program that it must create a file & it doesn’t has error
but at its runtime it has this error

Unhandled exception :system. Index out of range exception :index was
outside the bounds of the array

using System;
using System.IO;
namespace ConsoleApplication85
{
class Program
{
static void Main(string[] args)
{
StreamWriter myfile = File.CreateText(args[0]);
myfile.WriteLine("*********");
myfile.Close();
Console.WriteLine("****");


}
}
}


Because the array is empty. Right-click on your app in solution explorer and
select Debug then
enter any command line arguments you want to test. When you run the App from
the shell
the array will contain whatever you typed in there.

mick
 
F

Family Tree Mike

Hi all;
I wrote a program that it must create a file& it doesn’t has error
but at its runtime it has this error

Unhandled exception :system. Index out of range exception :index was
outside the bounds of the array

using System;
using System.IO;
namespace ConsoleApplication85
{
class Program
{
static void Main(string[] args)
{
StreamWriter myfile = File.CreateText(args[0]);
myfile.WriteLine("*********");
myfile.Close();
Console.WriteLine("****");


}
}
}

Did you pass a command line argument? You should have run something like:

ConsoleApplication85 SomeFile.txt

You would get the error if not including the SomeFile.txt part.
 
H

Harlan Messinger

sahel said:
static void Main(string[] args)
{
StreamWriter myfile = File.CreateText(args[0]);
Enter the debugger and take a look at args. Are you passing an argument to
the program?

:blush:
i don't know how to do that & i dont know anything a bout args ( i am
new at c# ) [ :-( ]

If you don't know what arguments are, how did it occur to you to write
"args[0]" and to expect that args[0] would contain the path of a file?

When you call an application myapp.exe at the command line as follows

C:\>myapp filepath

whatever you have in place of filepath is the first (and, in this case,
only) argument, and would be contained in arg[0]. If you are running
your app in Visual Studio, you can set the arguments to be used during
testing in the project's properties.
 
K

Konrad Neitzel

Hi Sahel!

you already got 2 good answers. I just want to add, that you should always
(!!) check the input first. You can never trust, what your public methods
get from outside.

In your case, you should check first, if args has an element.

Of course: You should simply grab some book and learn the basics but in my
eyes it is important to do these checks directly from the beginning.

Konrad
 

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