问题描述
我有一个采用命令行参数的VB.NET应用程序。
I have a VB.NET application that takes command-line arguments.
只要我关闭Visual Studio的ClickOnce安全设置,它在调试时就可以正常工作。
It works fine when debugging provided I turn off Visual Studio's ClickOnce security setting.
当我关闭尝试通过ClickOnce将应用程序安装在计算机上,并尝试使用参数运行它。
The problem occurs when I try to install the application on a computer via ClickOnce and try to run it with arguments. I get a crash when that happens (oh noes!).
此问题有一种解决方法:将文件从最新版本的publish文件夹移动到计算机的C:驱动器,然后从.exe中删除 .deploy。从C:驱动器运行应用程序,它将可以很好地处理参数。
There is a workaround for this issue: move the files from the latest version's publish folder to a computer's C: drive and remove the ".deploy" from the .exe. Run the application from the C: drive and it will handle arguments just fine.
有没有比我上面的解决方法更好的方法了?
Is there a better way to get this to work than the workaround I have above?
谢谢!
推荐答案
命令行参数仅适用于通过网址运行ClickOnce应用程序。
"Command-line arguments" only work with a ClickOnce app when it is run from a URL.
例如,这是您应启动应用程序以附加一些运行时参数的方式:
For example, this is how you should launch your application in order to attach some run-time arguments:
我有以下C#我用来解析ClickOnce激活URL和命令行参数的代码类似:
I have the following C# code that I use to parse ClickOnce activation URL's and command-line arguments alike:
public static string[] GetArguments()
{
var commandLineArgs = new List<string>();
string startupUrl = String.Empty;
if (ApplicationDeployment.IsNetworkDeployed &&
ApplicationDeployment.CurrentDeployment.ActivationUri != null)
{
// Add the EXE name at the front
commandLineArgs.Add(Environment.GetCommandLineArgs()[0]);
// Get the query portion of the URI, also decode out any escaped sequences
startupUrl = ApplicationDeployment.CurrentDeployment.ActivationUri.ToString();
var query = ApplicationDeployment.CurrentDeployment.ActivationUri.Query;
if (!string.IsNullOrEmpty(query) && query.StartsWith("?"))
{
// Split by the ampersands, a append a "-" for use with splitting functions
string[] arguments = query.Substring(1).Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries).Select(a => String.Format("-{0}", HttpUtility.UrlDecode(a))).ToArray();
// Now add the parsed argument components
commandLineArgs.AddRange(arguments);
}
}
else
{
commandLineArgs = Environment.GetCommandLineArgs().ToList();
}
// Also tack on any activation args at the back
var activationArgs = AppDomain.CurrentDomain.SetupInformation.ActivationArguments;
if (activationArgs != null && activationArgs.ActivationData.EmptyIfNull().Any())
{
commandLineArgs.AddRange(activationArgs.ActivationData.Where(d => d != startupUrl).Select((s, i) => String.Format("-in{1}:\"{0}\"", s, i == 0 ? String.Empty : i.ToString())));
}
return commandLineArgs.ToArray();
}
主要功能如下:
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
var commandLine = GetArguments();
var args = commandLine.ParseArgs();
// Run app
}
这篇关于ClickOnce应用程序将不接受命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!