Lucas Stanesa
Lucas Stanesa

Reputation: 71

C# File Association: passing double clicked file path to string

I recently made a notepad like program for C# and found a library for using file associations. I am wondering how I would pass the path of the file double clicked in explorer, to a string so the file can read and 'open' the text file (like how notepad does). I have googled for a while, and asked around a few forums, and my friends. Any answers or nudges in the right direction are appreciated. Thank You

(note: I've already tried reading it from the string[] args paramater in Main(), which was suggested by someone else)

EDIT: Solved, it was the args[0]. I was really tired when I started on this

Upvotes: 4

Views: 2692

Answers (3)

I've just made the following program

When opening a file with this program, it tells me the path of the file.

class Program
{
    static void Main(string[] args)
    {

        Console.WriteLine(args.Length);
        foreach (string s in args)
        {
            Console.WriteLine(s);
        }
        Console.ReadLine();

    }
}

the output for me was

1
C:\Users\MyUserName\Documents\Visual Studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\New Text Document.txt

you might want to execute a similar program, by using file.openWith, to see what happens.

Upvotes: 1

GETah
GETah

Reputation: 21449

This works fine for me!

public static void Main(string[] args){            
     if (args.Length == 0){
       // Show your application usage or show an error box              
       return;
     }
     string file = args[0];
     Application.Run(new MyProgram(file));           
}

Upvotes: 4

Surfbutler
Surfbutler

Reputation: 1528

The suggestion was correct, the filename you double-click on in explorer will be visible in your app as an args parameter. Then you can do what you like with it, such as open a file.

Upvotes: 3

Related Questions