C#中如何程序实现登陆局域网中某台计算机(用指定的用户名和密码)
我有个问题,搞了好几天也没搞出来,C#中如何程序实现登陆局域网中某台计算机(用指定的用户名和密码)有没有高手指点一二,我想用API(WNetAddConnection2),可总是出错,代码如下:
一、NetLink.cs
using System;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data;
using System.IO ;
using System.Net;
using System.Windows.Forms ;
using System.Management;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace MyNet
{
/// <summary>
/// 登陆网络上的计算机
/// </summary>
public class NetLink
{
/// <summary>
/// 登陆网络中的计算机
/// </summary>
/// <param name="UserName">用于登陆的用户名</param>
/// <param name="PassWord">用户密码</param>
/// <param name="ComputerName">要登陆的计算机名</param>
/// <returns>登陆成功返回true否false</returns>
public static bool Function_LoginNet(string UserName, string PassWord ,string ComputerName,string share,out int err)
{
/****用WMI登陆(没用到)state*******************
ConnectionOptions options = new ConnectionOptions();
options.Username = UserName;
options.Password = PassWord;
string str_computername = "\\\\"+ComputerName+"\\root\\cimv2";
ManagementScope scope = new ManagementScope(str_computername, options);
try
{
scope.Connect();
}
catch(Exception ex)
{
return false;
}
return true;
********** end ****/
/************* 用net命令连接网络 start ********
string str_computername = "\\\\"+ComputerName;
string str_netparamenter = "use ";
str_netparamenter += str_computername+" "+UserName+" "+PassWord;
Process pro = new Process();
pro.StartInfo.FileName = "net";
pro.StartInfo.Arguments = str_netparamenter;
//pro.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pro.Start();
pro.WaitForExit();
if(pro.ExitCode != 0)
{
pro.Dispose();
return false;
}
else
{
pro.Dispose();
return true;
}
***************** end ***************************/
//****用API(WNetAddConnection2)连接网络计算机
NETRESOURCE source = new NETRESOURCE();
//source.dwType = ResourceType.RESOURCETYPE_ANY;
source.dwType = 0;
source.lpRemoteName = "\\\\"+ComputerName;
if(share != "")
source.lpRemoteName += "\\"+share;
source.lpLocalName = null;
err = WNetAddConnection2(ref source, PassWord, UserName,1);
if(err != 0)
return false;
return true;
//******************************
}
public static void ShowNetLoginDialog(int hand)
{
WNetConnectionDialog(hand,0x01);
}
public enum ResourceScope
{
RESOURCE_CONNECTED = 1,
RESOURCE_GLOBALNET,
RESOURCE_REMEMBERED,
RESOURCE_RECENT,
RESOURCE_CONTEXT
};
public enum ResourceType
{
RESOURCETYPE_ANY,
RESOURCETYPE_DISK,
RESOURCETYPE_PRINT,
RESOURCETYPE_RESERVED
};
public enum ResourceUsage
{
RESOURCEUSAGE_CONNECTABLE = 0x00000001,
RESOURCEUSAGE_CONTAINER = 0x00000002,
RESOURCEUSAGE_NOLOCALDEVICE = 0x00000004,
RESOURCEUSAGE_SIBLING = 0x00000008,
RESOURCEUSAGE_ATTACHED = 0x00000010,
RESOURCEUSAGE_ALL = (RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED),
};
public enum ResourceDisplayType
{
RESOURCEDISPLAYTYPE_GENERIC,
RESOURCEDISPLAYTYPE_DOMAIN,
RESOURCEDISPLAYTYPE_SERVER,
RESOURCEDISPLAYTYPE_SHARE,
RESOURCEDISPLAYTYPE_FILE,
RESOURCEDISPLAYTYPE_GROUP,
RESOURCEDISPLAYTYPE_NETWORK,
RESOURCEDISPLAYTYPE_ROOT,
RESOURCEDISPLAYTYPE_SHAREADMIN,
RESOURCEDISPLAYTYPE_DIRECTORY,
RESOURCEDISPLAYTYPE_TREE,
RESOURCEDISPLAYTYPE_NDSCONTAINER
};
[StructLayout(LayoutKind.Sequential)]
public class NETRESOURCE
{
public ResourceScope dwScope = 0;
public ResourceType dwType = 0;
public ResourceDisplayType dwDisplayType = 0;
public ResourceUsage dwUsage = 0;
public string lpLocalName = null;
public string lpRemoteName = null;
public string lpComment = null;
public string lpProvider = null;
};
public enum ErrorCodes
{
NO_ERROR = 0,
ERROR_NO_MORE_ITEMS = 259
};
[DllImport("mpr.dll",EntryPoint="WNetAddConnection2", CallingConvention=CallingConvention.Winapi)]
private static extern int WNetAddConnection2(ref NETRESOURCE lpNetResource,string lpPassword,string lpUsername, System.UInt32 dwFlags );
[DllImport("mpr.dll", EntryPoint="WNetAddConnection")]
public static extern int WNetAddConnection(string lpszNetPath, string lpszPassword, string lpszLocalName) ;
[DllImport("mpr.dll")]
public static extern int WNetConnectionDialog(int hwnd, int dwType);
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct USER_INFO_3
{
[MarshalAs(UnmanagedType.LPWStr)]
public string usri3_name;
[MarshalAs(UnmanagedType.LPWStr)]
public string usri3_password;
public uint usri3_password_age;
public uint usri3_priv;
[MarshalAs(UnmanagedType.LPWStr)]
public string usri3_home_dir;
[MarshalAs(UnmanagedType.LPWStr)]
public string usri3_comment;
public uint usri3_flags;
[MarshalAs(UnmanagedType.LPWStr)]
public string usri3_script_path;
public uint usri3_auth_flags;
[MarshalAs(UnmanagedType.LPWStr)]
public string usri3_full_name;
[MarshalAs(UnmanagedType.LPWStr)]
public string usri3_usr_comment;
[MarshalAs(UnmanagedType.LPWStr)]
public string usri3_parms;
[MarshalAs(UnmanagedType.LPWStr)]
public string usri3_workstations;
public uint usri3_last_logon;
public uint usri3_last_logoff;
public uint usri3_acct_expires;
public uint usri3_max_storage;
public uint usri3_units_per_week;
public IntPtr usri3_logon_hours;
public uint usri3_bad_pw_count;
public uint usri3_num_logons;
[MarshalAs(UnmanagedType.LPWStr)]
public string usri3_logon_server;
public uint usri3_country_code;
public uint usri3_code_page;
public uint usri3_user_id;
public uint usri3_primary_group_id;
[MarshalAs(UnmanagedType.LPWStr)]
public string usri3_profile;
[MarshalAs(UnmanagedType.LPWStr)]
public string usri3_home_dir_drive;
public uint usri3_password_expired;
}
[DllImport("netapi32.dll", ExactSpelling=true)]
public extern static int NetApiBufferFree( IntPtr pBuf );
[DllImport("netapi32.dll", CharSet=CharSet.Unicode)]
public static extern int NetUserGetInfo( string srv, string usr, int lvl, out IntPtr pUI );
[DllImport("netapi32.dll", CharSet=CharSet.Unicode)]
public static extern int NetUserSetInfo( string srv, string usr,
int lvl, IntPtr pUI, out int param_err );
/// <summary>
/// 测试某一网络路径的属性
/// </summary>
/// <param name="netPath">要测试的网络路径</param>
/// <returns>返回NetLinkStatic枚举中的文件的状态</returns>
public static NetLinkStatic Function_TestNet(string netPath)
{
DirectoryInfo dir = new DirectoryInfo(netPath);
if(!dir.Exists)
{
return NetLinkStatic.None;
}
try
{
DirectoryInfo dirsub = dir.CreateSubdirectory("DAK test");
dirsub.Delete(true);
return NetLinkStatic.Write;
}
catch(Exception ex)
{
return NetLinkStatic.Read;
}
}
}
/// <summary>
/// 网络中共享目录的状态
/// </summary>
public enum NetLinkStatic
{
None,
Read,
Write
}
}