一、
以下是访问远程机器上的某个共享文件夹的例子:
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")
3.StreamReader mStm
=new StreamReader(@"\\192.168.0.1\C\test.Doc",System.Text.Encoding.Default);