0

I created a text editor in C# and I use a special file extension for the XML file that my program uses. When I use "Open With..." from the Windows context menu, my program doesn't read the file and I get an error.

How do I fix this?

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
khtaby
  • 41
  • 4

3 Answers3

2

In your Main() method, you need to capture the file name:

static void Main(string args[])
{
   string fileName;
   if (args.Length > 0)
      fileName = args[0];

   ...
}

Then you'll need to pass fileName to the code that opens the file. How you do that is up to you.

If your Main() method has no parameters, just add the string args[] parameter and the runtime will take care of populating the array with the commandline parameters.

If you are already doing that, then this is probably a SuperUser question.

Igby Largeman
  • 16,495
  • 3
  • 60
  • 86
  • but the Main like that static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } it doesn't have any parameters – khtaby Apr 13 '10 at 18:31
  • Add the `string args[]` parameter and the OS will provide the array of command line parameters. Both are valid signatures for `Main`. – Ron Warholic Apr 13 '10 at 18:48
  • @khtaby: just add the args[] parameter yourself (see edited question). – Igby Largeman Apr 13 '10 at 18:48
1

but the Main like that

static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

it doesn't have any parameters

khtaby
  • 41
  • 4
1

you can use this simple code to answer me

   private void button1_Click(object sender, EventArgs e)
    {
        richTextBox1.Text = File.ReadAllText(@"d:\wifi.txt");
    }

the text viewed in the richtextbox1

khtaby
  • 41
  • 4