如何以编程方式访问文件共享
我有一个 Windows 窗体应用程序在不在域上的计算机上运行,需要能够将文件从本地文件系统移动到 UNC 路径。我有该路径的用户名和密码。我想知道是否有任何方法可以直接执行此操作而不执行 net.exe
命令?
理想情况下,我不必映射驱动器。
I have a windows forms app running on a machine that is not on a domain, that needs to be able to move a file from the local filesystem to a UNC path. I have a username and password for that path. I was wondering is there any way to do this directly with out execing the net.exe
command?
Ideally I wouldn't have to map a drive.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 WNetAddConnection 来完成此操作。您必须 pInvoke。在我设置 pInvoke 声明后,下面的代码对我有用。第二个代码块(如下)包含 pInvoke 声明——只需将其粘贴到类中即可。
您需要将以下支持代码粘贴到一个类中(取自 pInvoke.Net)。确保在代码中添加 using 语句:
You can use WNetAddConnection to accomplish this. You will have to pInvoke. the code below worked for me after I set up the pInvoke declarations. The second block of code (below) contains the pInvoke declarations -- just stick it inside of a class.
You will need to paste the following supporting code into a class (taken from pInvoke.Net). Make sure to add a using statment to your code:
关于此问题的公认答案 这里似乎值得研究一下;它建议使用 Win32 API 函数 WNetUseConnection 。
来自 MSDN:
这似乎完成了您正在寻找的内容,但没有提及
net.exe
。这有帮助吗?The accepted answer on this question here seems like it would be worth looking into; it suggests using the Win32 API function WNetUseConnection.
From MSDN:
Which seems to accomplish what you're looking for, with no mention of
net.exe
. Does this help?