使用ShellExecute将参数传递给ClickOnce

时间:2013-09-24 16:39:37

标签: c# clickonce

我试图使用此Blog将参数传递给脱机点击一次应用程序。在博客底部附近有一些示例程序可供下载。我下载并发布了程序“TestRunningWithArgs”C#版本。

现在我正在尝试将参数传递给不起作用的应用程序。它一直说不“没有参数传入”。

我正在尝试使用以下代码传递参数:

shellexecute("File Path\My Applicaiton.application","MyArgument")

此代码运行应用程序,但示例应用程序声明它没有收到参数。语言是视觉基础(可能是4或6?)。

以下代码可以运行并允许Internet Explorer运行,并且可以从sharepoint打开特定文件,我想知道是否存在缺少的差异。

shellexecute("C:\Program Files\Internet Explorer\iexplore.exe",FileLocation)

我看到的唯一一个是点击一次是.application文件和.exe,有​​没有办法让它工作?

以下是Click once应用程序的代码,只是从博客上提供的示例程序中复制

public partial class MainForm : Form
{
public MainForm()
{
    InitializeComponent();
    GetArgsToShow();
}

/// Get the arguments and show them on the screen.
private void GetArgsToShow()
{
    //   Get the ActivationArguments from the SetupInformation property of the domain.
    string[] activationData = 
      AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData;

    if (activationData != null && activationData.Length > 0)
    {
        //querystring starts with ?; file association starts with "file:"
        if (activationData.Length == 1 && activationData[0].Substring(0, 1) == "?")
        {
            ProcessQueryString(activationData);
        }
        else if (activationData.Length == 1 && activationData[0].Length >= 5 && activationData[0].Substring(0,5).ToLower() == @"file:")
        {
            ProcessFileAssociation(activationData);
        }
        else 
        {
            ProcessCSVParameters(activationData);
        }
    }
    else
    {
        if (activationData == null)
        {
            lstArgs.Items.Add("No arguments passed in.");
        }
        else
        {
            lstArgs.Items.Add(String.Format("Number of args = {0}", activationData.Length));
        }
    }

}

/// Convert a query string into Name/Value pairs and process it.
private void ProcessQueryString(string[] activationData)
{
    NameValueCollection nvc = 
      System.Web.HttpUtility.ParseQueryString(activationData[0]);

    //Get all the keys in the collection, then pull the values for each of them.
    //I'm only passing each key once, with one value.
    string[] theKeys = nvc.AllKeys;
    foreach (string theKey in theKeys)
    {
        string[] theValue = nvc.GetValues(theKey);
        lstArgs.Items.Add(string.Format("Key = {0}, Value = {1}", theKey, theValue[0]));
    }                   
}

/// Process what you would get if you set up a file association, 
/// and the user double-clicked on one of the associated file.
private void ProcessFileAssociation(string[] activationData)
{
    //This is what you get when you set up a file association and the user double-clicks 
    //  on an associated file. 
    Uri uri = new Uri(activationData[0]);
    lstArgs.Items.Add(uri.LocalPath.ToString());
}

/// Process a comma-delimited string of values. Not: can't have spaces or double-quotes in the string,
/// it will only read the first argument.
private void ProcessCSVParameters(string[] activationData)
{
    //I have to say here that I've only ever seen 1 entry passed in activationData,
    //  but I'm checking for multiples just in case. 
    //This takes each entry and splits it by comma and separates them into separate entries.

    char[] myComma = { ',' };

    foreach (string arg in activationData)
    {
        string[] myList = activationData[0].Split(myComma, StringSplitOptions.RemoveEmptyEntries);
        foreach (string oneItem in myList)
            lstArgs.Items.Add(oneItem);
    }
}
}

修改 好吧,我尝试访问位于“C:\ Users \ userName \ AppData \ Local \ Apps \ 2.0 \ 5G9CGPWV.6O3 \ X7YPB07N.2Q2 \ test..tion_0000000000000000_0001.0000_03232931d88a66c9 \”中的.exe,它不起作用。

1 个答案:

答案 0 :(得分:0)

找到你要调用的文件来发送一个参数。

shellexecute("C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\(Publisher Name)\My Application.appref-ms", Arguement)

发布商名称在visual studio“发布”选项卡,选项和“发布者名称”中指定。这也是可以从中启动程序的开始菜单文件夹。