打开在另一个用户和域共享文件? [英] Open a shared file under another user and domain?
问题描述
我有需要读取机上的共享文件中的另一个域中的C#控制台应用程序。
当应用程序试图访问发生异常文件作为本地用户没有权限来访问共享资源。
I have a C# console application that needs to read a shared file on a machine in another domain. When the application tries to access the file an exception occurs as the local user does not have permission to access the shared resource.
目前我手动克服这个问题从运行打开共享文件夹,并把用户名和密码进入windows身份验证对话框,然后运行该应用程序。
Currently I overcome this problem manually by open the shared folder from the run and put the username and password into the windows authentication dialog then run the application.
我怎样才能做到这一点编程?
How can I do it programmatically?
推荐答案
A)的P / Invoke的 的LogonUser
与 LOGON32_LOGON_NEW_CREDENTIALS
并创建一个新的 的WindowsIdentity
新的令牌,然后使用正常的文件访问。
a) p/invoke LogonUser
with LOGON32_LOGON_NEW_CREDENTIALS
and create a new WindowsIdentity
with the new token, then use normal file access.
B)的P / Invoke WNetAddConnection3 。注意,这使得你的机器上的每一个其他进程访问远程共享。
b) p/invoke WNetAddConnection3. Be advised that this makes your remote share accessible to every other process on your machine.
C)通过 System.Management
WMI和的 CIM_DataFile
;你甚至不会需要的P / Invoke。 System.Management
允许您指定远程计算机凭据。
c) WMI via System.Management
and CIM_DataFile
; you won't even need p/invoke. System.Management
lets you specify credentials for remote machine.
这篇关于打开在另一个用户和域共享文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!