C# Windows 10-Peerfinder控制台应用程序

C# Windows 10-Peerfinder控制台应用程序,c#,win-universal-app,desktop-application,desktop,wifi-direct,C#,Win Universal App,Desktop Application,Desktop,Wifi Direct,我想在控制台应用程序(而不是通用应用程序)中的2个windows 10之间设置WiFiDirect连接 我将目标平台版本设置为10 10.0 < > >我添加了对Windows .Fund、WINDOWS.NETWORKING、Access、WINDOWS.NETWorks.Sosits、Windows .Sturial.Suths的引用。 在我的应用程序中,我检查WiFiDirect是否受支持 if ((Windows.Networking.Proximity.PeerFind

我想在控制台应用程序(而不是通用应用程序)中的2个windows 10之间设置WiFiDirect连接

  • 我将目标平台版本设置为10

    10.0

  • < > >我添加了对Windows .Fund、WINDOWS.NETWORKING、Access、WINDOWS.NETWorks.Sosits、Windows .Sturial.Suths

    的引用。 在我的应用程序中,我检查WiFiDirect是否受支持

            if ((Windows.Networking.Proximity.PeerFinder.SupportedDiscoveryTypes & Windows.Networking.Proximity.PeerDiscoveryTypes.Browse) != Windows.Networking.Proximity.PeerDiscoveryTypes.Browse)
            {
                Console.WriteLine("Peer discovery using Wifi-Direct is not supported.\n");
    
            }
    
    这是应该的。 但是当我调用PeerFinder.Start()时,我得到一个异常

    HRESULT 0x80004004(E_ABORT)
    
    这是调用PeerFinder.Start()的代码

    我想我不能在控制台应用程序中使用PeerFinder,有没有其他方法可以为桌面应用程序建立WiFiDirect连接?

    您必须先调用“SetCurrentProcessExplicitAppUserModelID”API。有关更多信息,请参阅本QA-您必须首先调用“SetCurrentProcessExplicitAppUserModelID”API。有关更多信息,请参阅本QA-
            try
            {
                PeerFinder.AllowWiFiDirect = true;
                PeerFinder.Role = PeerRole.Peer;
                PeerFinder.ConnectionRequested += PeerFinder_ConnectionRequested;
                PeerFinder.TriggeredConnectionStateChanged += PeerFinder_TriggeredConnectionStateChanged;
    
                using (var discoveryDataWriter = new Windows.Storage.Streams.DataWriter(new Windows.Storage.Streams.InMemoryRandomAccessStream()))
                {
                    discoveryDataWriter.WriteString("test12");
                    PeerFinder.DiscoveryData = discoveryDataWriter.DetachBuffer();
    
                }
    
                PeerFinder.Start();
            }
            catch (Exception e)
            {
                Console.WriteLine("start failed: " + e.Message);
            }