尝试从cmd进行编译时找不到元数据文件

时间:2019-07-05 23:14:14

标签: c# uwp win-universal-app windows-server-2019

这是我的样品:

using System;
using Windows.Networking.Vpn;
static void main()
{VpnManagementAgent mgr = new VpnManagementAgent();
 VpnNativeProfile profile = new VpnNativeProfile() { AlwaysOn = false,
    NativeProtocolType = VpnNativeProtocolType.L2tp,
    ProfileName = "MyConnection",
    RememberCredentials = true, RequireVpnClientAppUI = true,
    RoutingPolicyType = VpnRoutingPolicyType.SplitRouting,
    TunnelAuthenticationMethod = VpnAuthenticationMethod.PresharedKey,
    UserAuthenticationMethod = VpnAuthenticationMethod.Mschapv2, }; 
    profile.Servers.Add("vpn.example.com"); 
    VpnManagementErrorStatus profileStatus = await mgr.AddProfileFromObjectAsync(profile);
    Console.WriteLine($"{profileStatus}\n");  }

这是我尝试编译的方式(来自VS 2019的Developer Command Prompt):

csc program.cs /r:Windows.Networking.Vpn.dll

以下是我已安装的工具包的屏幕截图:

enter image description here

这是我的输出:

Microsoft (R) Visual C# Compiler version 3.100.119.28106 (58a4b1e7)
Copyright (C) Microsoft Corporation. All rights reserved.

error CS0006: Metadata file 'Windows.Networking.Vpn.dll' could not be found

这是msdn的链接:

  

程序集:Windows.Networking.Vpn.dll,Windows.dll

2 个答案:

答案 0 :(得分:1)

要查找的类型在Windows运行时元数据文件中定义,并在本机代码中实现。您需要引用winmd。有一些快捷方式(例如,引用通过操作系统安装的元数据),但这会使您的项目变脆。通常,您将要引用已安装的SDK版本。您可以从VS命令提示符中使用路径变量来帮助一点,例如(使用17763 SDK):

csc Program.cs \
-reference:"%WindowsSdkDir%\References\%WindowsSdkVersion%\Windows.Foundation.FoundationContract\3.0.0.0\Windows.Foundation.FoundationContract.winmd" \
-reference:"%WindowsSdkDir%\References\%WindowsSdkVersion%\Windows.Foundation.UniversalApiContract\7.0.0.0\Windows.Foundation.UniversalApiContract.winmd"

但是,这仍然会有些脆弱,因为这些路径中的合同版本号将随SDK升级而改变。 VS项目系统从“%WindowsSdkDir%\ Platforms \ UAP \%WindowsSdkVersion%\ Platform.xml”或“%WindowsSdkDir%\ Platforms \ UAP \%WindowsSdkVersion%\ PreviousPlatforms.xml”中读取当前合同,以获取正确的API信息目标操作系统版本。

答案 1 :(得分:0)

您的代码不正确。 Windows.Networking.Vpn名称空间适用于UWP应用程序,并且您在代码中尝试使用System.Console.WriteLine()函数,该函数在此上下文中无效。即使csc.exe抛出的唯一错误是找不到文件,但这也不是您的问题。尝试在Visual Studio中制作一个新的UWP应用程序,然后在Visual Studio中进行编译,因为这使它变得更加容易。