[英]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.