简体   繁体   English

无法使用 C# 和 .NET 4.8 以编程方式打开网络驱动器 (smb/samba)

[英]Can't open network drive (smb / samba) programatically with C# and .NET 4.8

I have problems opening a.network drive programmatically.我在以编程方式打开网络驱动器时遇到问题。 I have used the way described here: Access a Remote Directory from C# .我使用了此处描述的方式: 从 C# 访问远程目录 This is the proposed solution in many other Stackoverflow posts and on other sites.这是许多其他 Stackoverflow 帖子和其他站点上建议的解决方案。 And I have not seen any other way to do it.而且我还没有看到任何其他方法来做到这一点。

The proposed solution gives an exception: system error 67 "The.network name cannot be found".建议的解决方案给出了一个例外:系统错误 67“找不到网络名称”。

The problem is that the.network name is correct and can be found.问题是.network名是正确的,可以查到。 If I first make the connection in windows file explorer typing the same.network name, username and password, the files can be accessed from the code.如果我首先在 windows 文件资源管理器中输入相同的网络名称、用户名和密码进行连接,则可以从代码访问这些文件。

Code as I want to make it following the proposed solution, which is not working:我想按照建议的解决方案编写代码,但该解决方案不起作用:

    var SambaSharePath = @"\\images.eksempel.dk\archive\public";
    var SambaUsername = @"net\username";    
    var SambaPassword = ConfigurationManager.AppSettings["SambaPassword"];
    networkCredential = new NetworkCredential(SambaUsername, SambaPassword);    
    string filename = Path.Combine(SambaSharePath, imagePath);
    MemoryStream image = new MemoryStream();
        using (var x = new NetworkConnection(SambaSharePath, networkCredential))
        {
           var stream = new FileStream(filename, FileMode.Open);
           filename = Path.GetFileName(filename);
           stream.CopyTo(image);
           stream.Close();
        }
    return image; 

To see how NetworkConnection works, use the link above.要查看NetworkConnection的工作原理,请使用上面的链接。

Code which is working after the connection to the share is established in file explorer (but not before):在文件资源管理器中建立与共享的连接后工作的代码(但不是之前):

    ...
    string filename = Path.Combine(SambaSharePath, imagePath);
    var stream = new FileStream(filename, FileMode.Open);
    ...

I have tried any possible combinations of forward and backward slashes etc. for the sharename and username, to get the code working but nothing helps.我已经为共享名和用户名尝试了正斜杠和反斜杠等任何可能的组合,以使代码正常工作,但没有任何帮助。 So I have ruled out misspelling and formatting errors.所以我排除了拼写错误和格式错误。

I have tried google the exception, but none of the explanations gave meaning in the context.我试过谷歌例外,但没有一个解释在上下文中给出了意义。

Does anybody have an idea of how to get the code working and what the is the cause of the error?有没有人知道如何让代码工作以及错误的原因是什么?

The workaround is to first establish the connection with file explorer, but that doesn't work automatically with server restart.解决方法是首先与文件资源管理器建立连接,但这不会在服务器重启时自动起作用。

The problem was solved using this post: Why do these DLLs have two apparently identical entry points?使用这篇文章解决了这个问题: 为什么这些 DLL 有两个明显相同的入口点?

I had to make a change in the original solution .我不得不改变原来的解决方案

    [DllImport("mpr.dll")]
    private static extern int WNetAddConnection2(NetResource netResource, 
                                                 string password, 
                                                 string username, 
                                                 int flags);

Must be modified to:必须修改为:

    [DllImport("mpr.dll", EntryPoint = "WNetAddConnection2", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern int WNetAddConnection2(NetResource netResource,
            string password, string username, int flags);

It came down how the pathname and username string are handled.它归结为如何处理路径名和用户名字符串。 As ANSI or Unicode.作为 ANSI 或 Unicode。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用网络使用将samba服务器驱动器映射到C#中的某些本地驱动器? - how to map samba server drive to some local drive in C# using net use? 我在网络驱动器上有一个简单的c#可执行文件,我每天都会更新,但是如果人们让它在计算机上运行/打开则无法执行 - I have a simple c# executable on a network drive that I update daily, but can't if people leave it running/open on their machine 我不能使用第三方 dll C# ASP .NET 4.8 - I can't use a third-party dll C# ASP .NET 4.8 以编程方式在vb.net或c#中弹出和缩回CD驱动器 - Programatically ejecting and retracting the CD drive in vb.net or c# 访问Samba共享,无驱动器映射,使用IP地址 - C# - Access Samba Share, No Drive Mapping, With IP address - C# 无法使用C#/。NET中的服务帐户在Google驱动器中进行授权 - Can't authorize in Google drive using service account in C#/.NET 我可以使用 C#/.NET 以编程方式禁用窗口自动播放功能吗? - Can I disable window autoplay function programatically with C#/.NET? 在C#中以编程方式配置Win 7网络适配器 - Configure Win 7 Network Adapter Programatically in C# 将 C# .Net 4.8 升级到 .Net 5(或 6)后无法加载`PresentationFramework` - Cant load `PresentationFramework` after upgrade C# .Net 4.8 to .Net 5 (or 6) C# (.Net 4.8 Framework) How to change the querystring parameters in an API POST Call from header to body and open PDF in new tab - C# (.Net 4.8 Framework) How to change the querystring parameters in an API POST Call from header to body and open PDF in new tab
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM