Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

c# - How to get command line from a ClickOnce application?

Before publishing I went to Project -> Properties -> Options -> File Associations and added the extension ".hsp". Set an icon and a ProgID ("MyCompany.Document.1" for testing). After I published and installed, my .hsp files had the icon I set, so the file association should be properly set, but when I double clicked one of these files the application run and I expected the name of the file I double clicked to be in the command line. I tried reading the parameter passed to my Main function, tried Environment.CommandLine, and tried Environment.GetCommandLineArgs(), but the only thing I found was the application path. By the way I'm doing all this check before creating my main form in the Main function, just to test. The args parameter is empty and the other two only contain my app path.

This is the beginning of my Main function:

    static void Main(string[] args)
    {
        try
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
            MessageBox.Show("CommandLine -> " + Environment.CommandLine);
            foreach (string str in args) MessageBox.Show("args -> " + str);
            foreach (string str in Environment.GetCommandLineArgs()) MessageBox.Show("GetCommandLineArgs -> " + str);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When you publish an app with ClickOnce and then launch it by double-clicking an associated file, the path to that file actually gets stored here:

AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0]

See MSDN's documentation for it here:

http://msdn.microsoft.com/en-us/library/system.runtime.hosting.activationarguments.aspx

Plus a tutorial on adding file associations to "Published" projects:

http://blogs.msdn.com/b/mwade/archive/2008/01/30/how-to-add-file-associations-to-a-clickonce-application.aspx


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...