Reputation: 213
I'm creating a program that uses a specific file type and i've associated that file type with the program, but when i click the file, it only starts the program, but it doesn't load the file I clicked. How can I make my program load the file?
Upvotes: 2
Views: 266
Reputation: 1359
You can use the static void Main(string[] args)
to get the filename.
I'd do something like this:
somewhere in your code you should have a string variable where to store the retrieved filename private string filename;
static void Main(string[] args) {
// first check if there are arguments
if (args.length > 0)
{
// check if the filename has the right extension
if (args[0].EndsWith(".ext"))
{
// check for existence
if (System.IO.File.Exists(args[0]))
{
// it exists.. so store the filename in the previously defined variable
filename = args[0];
}
}
}
}
This way you can do one kind of logic when the filename variable has content and another kind of logic when it hasn't.
Hope you can use this.
Upvotes: 1
Reputation: 841
When you open a file you've associated with your application, the file path is passed into your program as a command line argument. You can load the file yourself like so:
string fileName = Environment.GetCommandLineArgs()[0];
Upvotes: 3
Reputation: 57573
In your app you have to use Main(string[] args)
and get from args params passed to your application.
Here it is an example:
static void Main(string[] args)
{
if (args.Length == 0) return;
string text = File.ReadAllText(args[0]);
}
Upvotes: 2