如何用代码实现对有用户名和密码的服务器的连接

spacerain 2005-05-07 10:18:41
问题是这样的:bs结构,在客户端用浏览器访问服务器上的一个共享文件夹时,总是需要先在客户机上先连通服务器,即:通过“开始”--〉运行--〉\\10.10.108.11--〉输入用户名和密码,然后才能通过浏览器访问服务器的共享文件夹。请问有没有什么办法直接在程序里通过代码来完成对服务器的登陆呢。多谢多谢!~!~!~
...全文
330 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qimini 2005-05-08
  • 打赏
  • 举报
回复
用Process和命令行net use命令结合起来实现
coveking 2005-05-08
  • 打赏
  • 举报
回复
先把动态的IP用域名解析出来,然后登陆定向与目录下,就行了
spacerain 2005-05-08
  • 打赏
  • 举报
回复
怎么木有人帮帮我呀,请大家帮个忙:)
BigIdiot628 2005-05-08
  • 打赏
  • 举报
回复
#region API中定义的类方法结构和枚举

public class WindowsNetwork
{
public WindowsNetwork()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

//声明WindowsNetwork相关函数
//取消网络映射
[DllImport("mpr.dll")]
public static extern WNetCancelConnection2ReturnCode WNetCancelConnection2(string strResourceName,bool bForce);
//public static extern WNetCancelConnection2ReturnCode WNetCancelConnection2(string strResourceName,ConnectionType connectionType,bool bForce);
//创建网络映射
[DllImport("mpr.dll")]
public static extern WNetAddConnection2ReturnCode WNetAddConnection2(ref NetResource netResource,string strPassword,string strUserName,ConnectionOption dwFlags);
}

//连接类型
public enum ConnectionType
{
CONNECT_DONT_UPDATE_PROFILE=0,
CONNECT_UPDATE_PROFILE=0x00000001,
CONNECT_UPDATE_RECENT=0x00000002,
CONNECT_TEMPORARY=0x00000004,
CONNECT_INTERACTIVE=0x00000008,
CONNECT_PROMPT=0x00000010,
CONNECT_NEED_DRIVE=0x00000020,
CONNECT_REFCOUNT=0x00000040,
CONNECT_REDIRECT=0x00000080,
CONNECT_LOCALDRIVE=0x00000100,
CONNECT_CURRENT_MEDIA=0x00000200,
CONNECT_DEFERRED=0x00000400,
CONNECT_RESERVED=unchecked((int)0xFF000000)
}

//函数WNetCancelConnection2的返回值
public enum WNetCancelConnection2ReturnCode
{
NO_ERROR=0, /*没有发生错误*/
ERROR_BAD_PROFILE=1206, /*The user profile is in an incorrect format. */
ERROR_CANNOT_OPEN_PROFILE=1205, /*The system is unable to open the user profile to process persistent connections. */
ERROR_DEVICE_IN_USE=2404, /*The device is in use by an active process and cannot be disconnected.*/
ERROR_EXTENDED_ERROR=1208, /*A network-specific error occurred. To obtain a description of the error, call the WNetGetLastError function.*/
ERROR_NOT_CONNECTED=2250, /*The name specified by the lpName parameter is not a redirected device, or the system is not currently connected to the device specified by the parameter. */
ERROR_OPEN_FILES=2401 /*There are open files, and the fForce parameter is FALSE.*/
}
//函数WNetAddConnection2的返回值
public enum WNetAddConnection2ReturnCode
{
NO_ERROR=0, /*没有发生错误*/
ERROR_ACCESS_DENIED=5, /*Access to the network resource was denied. */
ERROR_ALREADY_ASSIGNED=85, /*The local device specified by the lpLocalName member is already connected to a network resource. */
ERROR_BAD_DEV_TYPE=66, /*The type of local device and the type of network resource do not match. */
ERROR_BAD_DEVICE=1200, /*The value specified by lpLocalName is invalid. */
ERROR_BAD_NET_NAME=67, /*The value specified by the lpRemoteName member is not acceptable to any network resource provider, either because the resource name is invalid, or because the named resource cannot be located. */
ERROR_BAD_PROFILE=1206, /*The user profile is in an incorrect format.*/
ERROR_BAD_PROVIDER=1204, /*The value specified by the lpProvider member does not match any provider. */
ERROR_BUSY=170, /*The router or provider is busy, possibly initializing. The caller should retry. */
ERROR_CANCELLED=1223, /*The attempt to make the connection was cancelled by the user through a dialog box from one of the network resource providers, or by a called resource. */
ERROR_CANNOT_OPEN_PROFILE=1205, /*The system is unable to open the user profile to process persistent connections. */
ERROR_DEVICE_ALREADY_REMEMBERED=1202, /*An entry for the device specified by lpLocalName is already in the user profile. */
ERROR_EXTENDED_ERROR=1208, /*A network-specific error occurred. Call the WNetGetLastError function to obtain a description of the error. */
ERROR_INVALID_PASSWORD=86, /*The specified password is invalid and the CONNECT_INTERACTIVE flag is not set. */
ERROR_NO_NET_OR_BAD_PATH=1203, /*The operation cannot be performed because a network component is not started or because a specified name cannot be used. */
ERROR_NO_NETWORK=1222 /*The network is unavailable. */
}

//NETRESOURCE结构:The NETRESOURCE structure contains information about a network resource. The structure is returned during enumeration of network resources. NETRESOURCE is also specified when making or querying a network connection with calls to various Windows Networking functions.
public struct NetResource
{
public ResourceEnumerationScope dwScope;
public ResourceType dwType;
public ResourceDisplayType dwDisplayType;
public ResourceUsage dwUsage;
public string lpLocalName;
public string lpRemoteName;
public string lpComment;
public string lpProvider;
}
BigIdiot628 2005-05-08
  • 打赏
  • 举报
回复
public string m_IPC="ipc$";
private void button1_Click(object sender, System.EventArgs e)
{
NetResource nr;
nr.dwDisplayType=ResourceDisplayType.RESOURCEDISPLAYTYPE_GENERIC; //无用参数,这里赋值为了避免编译错误
nr.dwScope=ResourceEnumerationScope.RESOURCE_GLOBALNET; //无用参数,这里赋值为了避免编译错误
nr.dwType=ResourceType.RESOURCETYPE_DISK; //有用参数
nr.dwUsage=ResourceUsage.RESOURCEUSAGE_CONNECTABLE; //无用参数,这里赋值为了避免编译错误
nr.lpComment=""; //无用参数,这里赋值为了避免编译错误
//nr.lpLocalName=txtWatchPathName.Text.Trim().Substring(0,2); //有用参数,需要将网络驱动器映射成本地的哪个盘符
nr.lpLocalName="";
nr.lpProvider=""; //有用参数
nr.lpRemoteName=txtNetworkComputerPathName.Text.Trim(); //有用参数,网络邻居的共享目录名(例如“\\DianNao1\MuLu”)
//if(WindowsNetwork.WNetAddConnection2(ref nr,txtNetworkComputerUser.Text.Trim(),txtNetworkComputerPassword.Text.Trim(),ConnectionOption.CONNECT_INTERACTIVE)==WNetAddConnection2ReturnCode.NO_ERROR)
if(WindowsNetwork.WNetAddConnection2(ref nr,txtNetworkComputerUser.Text.Trim(),txtNetworkComputerPassword.Text.Trim(),ConnectionOption.CONNECT_INTERACTIVE)==WNetAddConnection2ReturnCode.NO_ERROR)
{
MessageBox.Show("已经成功的映射网络驱动器","测试网络映射提示");
}
else
MessageBox.Show("映射网络驱动器失败","测试网络映射提示");
}
private void button2_Click(object sender, System.EventArgs e)
{
//if(WindowsNetwork.WNetCancelConnection2("z:",0,true)==WNetCancelConnection2ReturnCode.NO_ERROR)
if(WindowsNetwork.WNetCancelConnection2("",true)==WNetCancelConnection2ReturnCode.NO_ERROR)
MessageBox.Show("已经成功的断开的刚才创建的网络驱动器。","测试网络映射提示");
else
MessageBox.Show("在断开刚才创建的网络驱动器时失败。","测试网络映射提示");
}
BigIdiot628 2005-05-08
  • 打赏
  • 举报
回复
上次不记得是哪位好心人给了我代码?发给你吧?
net use的就免了吧。
是API的,我也看不懂。你慢慢的改吧?
miqier 2005-05-08
  • 打赏
  • 举报
回复
Process p;
p=new Process();
p.StartInfo.FileName="cmd.exe";

// 这里是关键点,不用Shell启动/重定向输入/重定向输出/不显示窗口
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;

p.Start();
// p.StandardInput.WriteLine("net use " + server + " " + pass + " /user:" + user);// 向cmd.exe输入command
p.StandardInput.WriteLine("net use \\\\192.168.0.1 Com#work /user:Administrator");// 向cmd.exe输入command
p.StandardInput.WriteLine("exit");
p.WaitForExit();
// string s = p.StandardOutput.ReadToEnd();// 得到cmd.exe的输出
p.Close();
shiafei 2005-05-08
  • 打赏
  • 举报
回复
可怜你,高手太多,可是出手的为什么总是我们这些不懂的呢???
spacerain 2005-05-08
  • 打赏
  • 举报
回复
Process app=new Process();
string netCMD=" use \\\\10.10.131.252"+"\\ReportFile "+" 3553 /user:administrator";
ProcessStartInfo info=new ProcessStartInfo(@"net",netCMD);
info.RedirectStandardError=true;
info.RedirectStandardOutput=true;
info.UseShellExecute=false;
info.CreateNoWindow = true;
app.StartInfo=info;
app.Start();
app.WaitForExit();

请问为什么执行到app.Start(); 时会捕捉到"拒绝访问"的错误呢,两位高手请指教.

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧