问题描述
在我的应用程序中,我使用 FileStream
来读取文件,该文件位于网络中某个文件共享上。因此,我的 remoteFilePath
变量类似于: \\计算机名\文件夹\file.pdf
In my application I use FileStream
to read from a file, that is on the fileshare somewhere in the network. So my remoteFilePath
variable is something like: \\computername\folder\file.pdf
FileStream fileStream = new FileStream(remoteFilePath, FileMode.Open, FileAccess.Read, FileShare.None, 1024 * 1024)
不幸的是,与我运行此应用程序的用户(与我登录PC的用户)没有访问此文件共享。我还有另一个用户(域,登录名和密码),可以访问这些文件。
Unfortunately, the user that I'm running this application with (that I'm logged into the PC with) does not have access to this fileshare. I have another user (domain, login & password), that has access to those files.
是否可以使用其他用户凭据将文件保存到文件流?我可以仅模拟用户 来获取文件,然后继续使用自己的用户吗?
Is it possible to use the other user credentials to get a file to filestream? Can I impersonate the user only to get a file, and then continue with my own user?
推荐答案
感谢您的回答。由于共享位于另一个域,因此模拟起来并不容易。
Thank you for your answers. Since the share was in another domain, it was not so easy to impersonate.
我找到了另一个更简单的解决方案。我映射了一个网络驱动器,并检查了选项使用不同的凭据连接
。然后,我连接到该驱动器,而不是远程路径。
I found another, easier solution. I mapped a network drive, and checked the option Connect using different credentials
. Then I connect to this drive instead of the remote path.
string mappedFilePath = filePath.Replace(@"\\computername\", @"Y:\")
并在<$ c中使用此新字符串$ c> FileStream 构造函数。
这篇关于使用另一个用户凭据从远程路径将文件获取到FileStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!