.net使用用户名和密码访问远程共享文件夹的两种方法

一、
以下是访问远程机器上的某个共享文件夹的例子:
E盘的目录清单。
try
{
ManagementScope ms = new ManagementScope(@"\\192.168.8.12");
ConnectionOptions conn = new ConnectionOptions();
conn.Username = "用户名";
conn.Password = "口令";
ms.Options = conn;

ms.Connect();
// ManagementObject disk = new ManagementObject(ms,new ManagementPath("Win32_logicaldisk='d:'"),null);
// disk.Get();
DirectoryInfo di = new DirectoryInfo(@"\\192.168.8.12\e$");
StringBuilder sb = new StringBuilder();
foreach(DirectoryInfo subDIR in di.GetDirectories())
{
sb.Append(subDIR.Name + "\n\r");

}
MessageBox.Show("OK\n\r" + sb.ToString());
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

另参考:
private void button1_Click(object sender, System.EventArgs e)

              {

                     ConnectionOptions co = new ConnectionOptions();

                     co.Username = "Administrator";

                     co.Password = "xxxxxxxxxxxxxxxxxxxx";

                     System.Management.ManagementScope ms = new System.Management.ManagementScope("218.241.42.116//root//cimv2", co);

              }

 

              private void button2_Click(object sender, System.EventArgs e)

              {

                     Process.Start("litao-mclpal8fv//temp//text.txt");

                     //Process.Start("d://temp//text.txt");

 

              }



二、原帖地址 http://blog.csdn.net/godpreserve/article/details/2862347

using System.Runtime.InteropServices;
using System;
using System.ComponentModel;
using System.IO;
enum LogonType : uint
    {
        
        Interactive = 2,
       
        Network = 3,
        
        Batch = 4,
       
        Service = 5,
        
        Unlock = 7,
        
        NetworkClearText = 8,
        
        NewCredentials = 9
    }
enum LogonProvider : uint
    {
        
        Default = 0,
        
        WinNT35 = 1,
        
        WinNT40 = 2,
       
        WinNT50 = 3,
    }
class IdentityScope : IDisposable
    {
        [DllImport("Advapi32.dll")]
        static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword,
            LogonType dwLogonType, LogonProvider dwLogonProvider, out IntPtr phToken);
        [DllImport("Advapi32.DLL")]
        static extern bool ImpersonateLoggedOnUser(IntPtr hToken);
        [DllImport("Advapi32.DLL")]
        static extern bool RevertToSelf();
        [DllImport("Kernel32.dll")]
        static extern int GetLastError();

        bool disposed;

        public IdentityScope(string domain, string userName, string password): this(domain, userName, password, LogonType.Interactive, LogonProvider.Default)
        {
        }

        public IdentityScope(string domain, string userName, string password, LogonType logonType, LogonProvider logonProvider)
        {
            if (string.IsNullOrEmpty(userName))
            {
                throw new ArgumentNullException("userName");
            }
            if (string.IsNullOrEmpty(domain))
            {
                domain = ".";
            }

            IntPtr token;
            int errorCode = 0;
            if (LogonUser(userName, domain, password, logonType, logonProvider, out token))
            {
                if (!ImpersonateLoggedOnUser(token))
                {
                    errorCode = GetLastError(); 
                }
            }
            else
            {
                errorCode = GetLastError();
            }
            if (errorCode != 0)
            {
                throw new Win32Exception(errorCode);
            }
        }

        ~IdentityScope()
        {
            Dispose(false);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    // Nothing to do.
                }
                RevertToSelf();
                disposed = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }

public class test
{
    //下面是一个测试函数:
    public static void aaa()
    {
        using (new IdentityScope(@"10.0.0.68", "aaa", "qwert", LogonType.NewCredentials, LogonProvider.WinNT50))
        {
            File.Copy("10.0.0.68//autotxt//2008-3-24 17.22.28.txt", @"C:/rere.txt");
       }
    }
}

三、

1.System.Diagnostics.Process.Start("net.exe","use \\\\"+ "192.168.0.1"+"  /user:\""+ strUserName +"\" \""+ strUserPD +"\"");
(vb.net: System.Diagnostics.Process.Start("net.exe","use \\192.168.0.1 password  /user:strUserName")

2.
3.StreamReader mStm

=new StreamReader(@"\\192.168.0.1\C\test.Doc",System.Text.Encoding.Default);

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值