调用 System.Diagnostics.Process.Start() 真的有问题吗? [英] Is calling System.Diagnostics.Process.Start() truly problematic?

查看:29
本文介绍了调用 System.Diagnostics.Process.Start() 真的有问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在准备我的应用程序以提交到 Microsoft Store(我的应用程序通过警告")时获得的验证结果中,我得到了这个失败"在包装健全性测试"下部分:

包含这样一个调用的代码是:

private void myMapsHelpToolStripMenuItem_Click(object sender, EventArgs e){System.Diagnostics.Process.Start(https://ramblingnotesofageezer.substack.com/p/map-o-matic-overview");}

我正在使用该代码,因为这里建议这样做.

事实上,即使我注释掉有问题的行,项目也将不再编译,但没有给我任何有关如何解决问题的信息,仅此:

我将包管理格式设置为 PackageReference,并安装了 Microsoft.Windows.SDK.Contracts 的 10.0.18362.2005 版本,但它抱怨需要包引用......?!?我尝试使用 Windows.System 添加;并使用 Microsoft.Windows.SDK.Contracts;但两者都未被识别.

为项目安装了包,如下所示:

更新 3

关于必须使用的包参考"错误消息,围绕我在这里看到的内容,我有三个问题:

Microsoft.Windows.SDK.Contracts 下面的文字说我可以更新这个包的版本 - 我应该吗?

我在我的项目的参考中没有看到对 Microsoft.Windows.SDK.Contracts 的参考,尽管它已经安装.我是否需要添加一个 - 如果是,从哪里添加?

我的 References 上的上下文菜单使我能够将 packages.config 迁移到 PackageReference...";我应该这样做吗?

解决方案

如果你正在开发一个 uwp 应用程序,为了打开一个 web uri,建议使用

2.安装Microsoft.Windows.SDK.Contracts包,注意需要安装合适的版本.请检查下面的相应版本.(安装详细信息:右键单击参考->管理 NuGet 包->浏览->搜索 Microsoft.Windows.SDK.合同->安装)

  • 10.0.19041.xxxx:为 Windows 10,版本 2004,版本 20H2 选择此项

  • 10.0.18362.xxxx:为 Windows 10 版本 1903 选择此项.

  • 10.0.17763.xxxx:为 Windows 10 版本 1809 选择此项.

  • 10.0.17134.xxxx:为 Windows 10 版本 1803 选择此项.

3.运行项目

In the Validation Result that I get when readying my app for submission to the Microsoft Store (which my app "PASSED WITH WARNINGS"), I got this "Fail" under the "Package Sanity Test" section:

The code which contains such a call is:

private void myMapsHelpToolStripMenuItem_Click(object sender, EventArgs e)
{
    System.Diagnostics.Process.Start("https://ramblingnotesofageezer.substack.com/p/map-o-matic-overview");
}

I am using that code due to the recommendation to do so here. How do I start a process from C#?

Is this truly a problem? If so, what should I use instead of the call to System.Diagnostics.Process.Start()?

I find it odd that it is classified as a failed part of the test, yet the overall results are that my app passed (albeit with warnings).

UPDATE

I checked out the link in the comment from Codexer, where it says, "Starting a utility can often provide a convenient way to obtain information from the operating system, access the registry, or access system capabilities. However, you can use UWP APIs to accomplish these sorts of tasks instead."

If this is the solution, do the UWP APIs have an equivalent to System.Diagnostics.Process.Start()?

UPDATE 2

I followed the steps in the answer, but the solution still doesn't compile, due to an error with this line of code:

await Windows.System.Launcher.LaunchUriAsync(uri);

In fact, even when I comment out the offending line, the project will no longer compile, but doesn't give me any information about how to solve the problem, just this:

I set the Package Management Format to PackageReference, and installed version 10.0.18362.2005 of Microsoft.Windows.SDK.Contracts, but it is complaining about needing a package reference...?!? I tried adding using Windows.System; and using Microsoft.Windows.SDK.Contracts; but neither is recognized.

The package is installed for the project, as you can see here:

UPDATE 3

Regarding the "Must Use Package Reference" err msg, I have three questions revolving around what I see here:

The verbiage below Microsoft.Windows.SDK.Contracts says I can update versions of this package - should I?

I do not see a Reference to Microsoft.Windows.SDK.Contracts in my project's References, although it has been installed. Do I need to add one - if so, from where?

The context menu on my References affords me the ability to "Migrate packages.config to PackageReference..." should I do that?

解决方案

If you were developing an uwp application, for opening open a web uri, it is recommended to use Launcher.LaunchUriAsync(Uri) method instead, this method starts the default browser to open the specified URI.

For example:

private async void button_Click(object sender, RoutedEventArgs e)
        {           
            Uri uri = new Uri("http://www.google.com");
            await Windows.System.Launcher.LaunchUriAsync(uri);
        }

Update:

As Codexer mentioned, you could refer to this document to use uwp api in winform app.

I have created a winform project, its target framework is .Net Framework4.8. For earlier versions of .NET(.NET Core 3.x, .NET 5 Preview 7 (or earlier), or .NET Framework), you could refer to my steps.

1.Click Tools->NuGet Package Manager-> Package Manager Settings-> Change Default package management format to PackageReference. As follows:

2.Install the Microsoft.Windows.SDK.Contracts package, note that you need to install the appropriate version. Please check the corresponding version below. (Installation details: Right click Reference-> Manage NuGet Packages->Browse->Search Microsoft.Windows.SDK.Contracts->install)

  • 10.0.19041.xxxx: Choose this for Windows 10, version 2004, version 20H2

  • 10.0.18362.xxxx: Choose this for Windows 10, version 1903.

  • 10.0.17763.xxxx: Choose this for Windows 10, version 1809.

  • 10.0.17134.xxxx: Choose this for Windows 10, version 1803.

3.Run the project

这篇关于调用 System.Diagnostics.Process.Start() 真的有问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆