如何在UWP中访问本地网络(SMB)? [英] How to access local network (SMB) in UWP?

查看:189
本文介绍了如何在UWP中访问本地网络(SMB)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经寻找了很长一段时间,但是对于该特定问题我找不到任何可行的解决方案.我的目标是为主要的HoloLens应用程序创建一个后台应用程序(这意味着它必须在UWP中完成),该应用程序应该能够访问本地文件和本地网络(SMB),以便可以在其中映射文件层次结构.以便将它们中继到Unity中的主应用程序,它将以3D/2D方式显示文件.

I am looking for quite a long time now, but I just couldn't find any working solution for this particular issue. My goal is to create a background application for the main HoloLens application (which means it must be done in UWP), which should be able to access both, local files and local networks (SMB), so that it can map the file hierarchy in order to relay them to the main application in Unity, which would display the files in a 3D/2D manner.

直到现在我已经尝试了几件事,包括:常规的StorageFolder/StorageFile交互(如果已经实现,那将是一件很不错的事情,因为已经实现了本地文件交互),SharpCifs.Smb和Xb.Net.SmbTree.

Until now I tried several things, including: Regular StorageFolder/StorageFile interactions (it would be great if this one worked, as local file interactions are already implemented), SharpCifs.Smb and Xb.Net.SmbTree.

SharpCifs:

SharpCifs:

    public SmbFile GetFile ()
    {
        //smb://UserName:Password@ServerIP/ShareName/Folder/FileName.txt
        SmbFile file = new SmbFile ("smb:\\winAccName:password@123.45.67.11\\TestShare");
        file.Connect (); // Or any file action.
        return file;
    }
    // Exception User-Unhandled -- SharpCifs.Util.Sharpen.UnknownHostException: '..MSBROWSE__.<01>'

直到现在,我还没有成功完成与SmbFile相关的所有操作.通常我会收到错误消息,提示它无法连接到IP地址.

Until now I didn't succeed to do anything with everything related to SmbFile. Usually I get the error message that it is unable to connect to the IP address.

使用StorageFile/文件夹:

With StorageFile/Folders:

    async public void OpenFile ()
    {
        StorageFolder folder = await StorageFolder.GetFolderFromPathAsync ("\\\\ServerName.local");
        if (folder != null) CreateMessage ("Got the folder: " + folder.Name);

        StorageFile file = await folder.GetFileAsync ("abc.txt");
        CreateMessage ("Success: " + file.Path);
    }
    // Exception User-Unhandled -- System.Runtime.InteropServices.COMException: 'Error HRESULT E_FAIL has been returned from a call to a COM component.'

后一个成功获得了文件夹,但没有对其做任何事情.但这是我迄今为止能做到的最远的.我还启用了以下功能:

The latter one succeeded to get the folder, but failed to do anything with it. But that's the furthest I could get so far. I also enabled the capabilities:

<Capabilities>
    <Capability Name="internetClientServer" />
    <Capability Name="internetClient" />
    <Capability Name="privateNetworkClientServer" />
    <uap:Capability Name="sharedUserCertificates" />
    <uap:Capability Name="enterpriseAuthentication" />
    <DeviceCapability Name="proximity" />
</Capabilities>

我假设我遇到了权限问题,我想知道为什么SmbTree/SmbFile交互始终需要用户名和密码,而StorageFile/StorageFolder交互却不要求用户名(因此失败?).我看到必须添加功能...但这并不能满足所有需求.文件选择器还是解决本地文件权限问题的解决方案,但不能成为网络解决方案(客户端不能允许其自身访问主机上的文件).另外,对于HoloLens,我不能依靠文件选择器,因为它目前没有.无论如何,选择器似乎仍然可以正常工作,如果我可以创建一个自定义文件选择器,那也是可以接受的解决方案……如果可能的话.

I assume I got permission issues, and I wonder why SmbTree / SmbFile interactions require usernames and passwords all the while StorageFile/StorageFolder interactions do not ask for it (and therefore fail?). I see that the capabilities have to be added... but that can't be all that's needed. The file picker is also just a solution to solve local file permission issues, but can't be the solution for networks (a client can't allow itself to access files on the host). Plus for HoloLens I can't rely on file pickers, as it currently has none. Anyway, the picker still seemed to work, and if I could create a custom file picker, that would be an acceptable solution as well... if that's possible.

Ps:我还试图以管理员身份运行Visual Studio.并且还设置了共享文件夹权限(甚至对于来宾也是如此).当前共享文件夹是可通过"A:\"访问的本地文件夹.还设置了文件类型关联.

Ps: I also tried to run Visual Studio as admin. And the shared folder permissions are also set up (even for guests). The current shared folder is a local folder accessible through "A:\". File type associations are also set.

我听到很多评论说UWP不支持SMB,但是我听到有关使它起作用的人的评论(尽管这对我不起作用).我还听说SMB 1不再被允许.我非常感谢您的帮助,因为这当然不是我的专业领域.

I heard quite some comments saying that SMB is not supported on UWP, yet I hear comments about people who made it work (which didn't work for me though). I also heard SMB 1 is not allowed any more. I'd be really grateful for any help, as this is certainly not my domain of expertise.

问候!

推荐答案

只是回答我自己的问题,好像我解决了这个问题,所以我想总结一下我所做的事情(我还发现了一部分甚至是必要的,但也许只是暂时性地继续起作用……无论哪种方式,这些都可能仍会提示):

Just to answer my own question, as it seems like I resolved it, I'd like to summarize what I did (I also found out only a part of that was even necessary, but maybe that continued to work only temporarily... either way these may still give a hint):

  • 在Package.appxmanifest中添加以下功能:Internet,专用网络,邻近性,企业身份验证.
  • 在此处,还添加声明:文件类型给定文件的关联并添加共享目标".这些内容可以在可视显示中进行设置,无需查看代码.
  • 确保在共享文件夹中正确设置了权限,并考虑来宾权限.这是强制性的.
  • 为以下项使用正确的路径:. . . StorageFolder文件夹=等待StorageFolder.GetFolderFromPathAsync(@"\\ 142.24.56.10 \ TestShare");或(@"\\ JOHNS-COMPUTER \ TestShare");

->从那里开始,您将拥有基于您的计算机和Windows帐户授予的权限(至少我认为是这样).

-> From there on you have the permissions granted based on your computer and windows account (at least I assume).

没有其他必要:既不需要特殊身份验证,也不需要SMB特定的库.它只能通过StorageFile/StorageFolder系统工作,并且可以使用与本地文件系统相同的方法来导航网络.

No other things were necessary: Neither special authentification nor SMB specific libraries. It just works through the StorageFile/StorageFolder system, and the network can be navigated with the same methodology as the local file system.

似乎罪魁祸首实际上是我的共享目标"声明.如果您声明了0个文件类型关联,或者您没有声明"Share Target",则它将不起作用.

It seems like the culprit actually was the "Share Target" declaration for me. If you have 0 file type associations declared or if you did not declare "Share Target", it won't work.

另一个信息:"broadFileSystemAccess"也非常有用,它可以减轻您对标准文件类型的限制.没有这个,您将无法获取未声明的文件和没有扩展名的文件.不过,它有自己的规则,请仔细阅读.

Another info: "broadFileSystemAccess" is quite useful as well, which alleviates you from the standard file type restrictions. Without this you won't be able to get non-declared files and files without extension. It has it's own rules though, read it up.

一个小提示:Visual Studio在xml中在绿色下划线,好像有一个错误,但仍然有效.

One little hint: Visual Studio underlined it green in the xml as if there was an error, but it still worked.

这篇关于如何在UWP中访问本地网络(SMB)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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