Reputation: 1000
I have a program that has a file type associated with it (*.cqd). When the user double clicks a .cqd file, it opens up my program just fine, but I can't for the life of me get the name/path of the file used to open the program.
I've tried the following code:
public static void Main(string[] args)
{
foreach (string s in args)
{
MessageBox.Show("MAIN: " + s);
}
}
Which only gives me the path of the program itself.
I've also tried:
foreach (string arg in Environment.GetCommandLineArgs())
{
MessageBox.Show(arg);
}
Which has the same effect. I'm having a hard time finding information on the topic because my searches give me "How do I open a file with it's associated program?" rather than the problem I'm having. Any help I can get would be appreciated.
NINJA EDIT: This is not a WPF project. Sorry I wasn't specific on that. I also fixed a quick typo.
FOUND THE PROBLEM! When I publish the application, it publishes to a .application file. I went into the project's /bin/release/ folder and found the .exe file. When I drag a file onto the .exe, it properly passes the path into the arguments. Guess I need to read up more on why this is, and if I can get the .application file to work with arguments, seeing as how it has the auto-update of ClickOnce in it.
Thank you all for your help! If there's anything I need to do to close this thread, please let me know.
Upvotes: 2
Views: 5065
Reputation: 4013
In App.xaml.cs
protected override void OnStartup(StartupEventArgs e)
{
if (e.Args != null && e.Args.Length > 0)
{
//MessageBox.Show("dgda");
//DirectOpenPath = e.Args[0].ToString();
this.Properties["ArbitraryArgName"] = e.Args[0];
}
base.OnStartup(e);
}
In MainWindow.xaml.cs
InitializeComponent();
if (Application.Current.Properties["ArbitraryArgName"] != null)
{
string fname = Application.Current.Properties["ArbitraryArgName"].ToString();
// Act on the file...
}
Upvotes: 2
Reputation: 590
AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0]
Works as well for ClickOnce activations
Upvotes: 4
Reputation: 1271
This blog post details your exact problem, and the solution.
But, in short, it's not stored in args, it's stored in:
AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0]
(This is all assuming you're using WPF)
Upvotes: 1
Reputation: 59533
Perhaps you meant:
public static void Main(string[] args)
{
foreach (string s in args)
{
MessageBox.Show("MAIN: " + s);
}
}
Upvotes: 2