C#调用advapi32.dll访问需要账号密码登录的共享文件夹

C#调用advapi32.dll,访问需要账号密码登录的共享文件夹里的文件

[DllImport("advapi32.DLL", SetLastError = true)] //Import advapi32.dll

//方法声明
 public static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

//登录并读写文件

                string userName = "共享文件夹登录账号"; 

                string password = "共享文件夹登录密码";
                IntPtr admin_token = default(IntPtr);
                WindowsIdentity wid_admin = null;
                WindowsImpersonationContext wic = null;
                
                int result = LogonUser(userName, "共享文件夹主机名", password, 9, 0, ref admin_token);
                if (result != 0)
                {
                    using (wid_admin = new WindowsIdentity(admin_token))
                    {
                        using (wic = wid_admin.Impersonate())
                        {

                                //登录成功

                                //对共享文件夹里的文件进行读写操作,路径即为共享文件路径

                        }

                    }

                }

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
“ConsoleApplication2.exe”(Win32): 已加载“D:\vsceshi\ConsoleApplication2\x64\Debug\ConsoleApplication2.exe”。已加载符号。 “ConsoleApplication2.exe”(Win32): 已加载“C:\Windows\System32\ntdll.dll”。无法查找或打开 PDB 文件。 “ConsoleApplication2.exe”(Win32): 已加载“C:\Windows\System32\kernel32.dll”。无法查找或打开 PDB 文件。 “ConsoleApplication2.exe”(Win32): 已加载“C:\Windows\System32\KernelBase.dll”。无法查找或打开 PDB 文件。 “ConsoleApplication2.exe”(Win32): 已加载“C:\Windows\System32\msvcp140d.dll”。无法查找或打开 PDB 文件。 “ConsoleApplication2.exe”(Win32): 已加载“C:\Windows\System32\vcruntime140d.dll”。无法查找或打开 PDB 文件。 “ConsoleApplication2.exe”(Win32): 已加载“C:\Windows\System32\vcruntime140d.dll”。无法查找或打开 PDB 文件。 “ConsoleApplication2.exe”(Win32): 已加载“C:\Windows\System32\ucrtbased.dll”。无法查找或打开 PDB 文件。 “ConsoleApplication2.exe”(Win32): 已卸载“C:\Windows\System32\vcruntime140d.dll” “ConsoleApplication2.exe”(Win32): 已加载“C:\Windows\System32\advapi32.dll”。无法查找或打开 PDB 文件。 “ConsoleApplication2.exe”(Win32): 已加载“C:\Windows\System32\ucrtbased.dll”。无法查找或打开 PDB 文件。 “ConsoleApplication2.exe”(Win32): 已卸载“C:\Windows\System32\ucrtbased.dll” “ConsoleApplication2.exe”(Win32): 已加载“C:\Windows\System32\msvcrt.dll”。无法查找或打开 PDB 文件。 “ConsoleApplication2.exe”(Win32): 已加载“C:\Windows\System32\sechost.dll”。无法查找或打开 PDB 文件。 “ConsoleApplication2.exe”(Win32): 已加载“C:\Windows\System32\rpcrt4.dll”。无法查找或打开 PDB 文件。 “ConsoleApplication2.exe”(Win32): 已加载“C:\Windows\System32\cryptbase.dll”。无法查找或打开 PDB 文件。 “ConsoleApplication2.exe”(Win32): 已加载“C:\Windows\System32\kernel.appcore.dll”。无法查找或打开 PDB 文件。
07-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值