如何访问同一局域网内的其他电脑文件

来源:互联网 发布:最好的期货软件 知乎 编辑:程序博客网 时间:2024/04/28 06:31
 如何访问同一局域网内的其他电脑文件
此处为作者写的一个类:
using System.Collections.Specialized;
///<summary>
/// get hbldocument folder, intranet
///</summary>
/// 2006-11-08 Jason
///<param name="remoteHost">computer ip or name</param>
///<param name="userName">user name</param>
///<param name="passWord">password</param>
///<returns>true:success;</returns>
public bool ConnectNet(string remoteHost, string userName, string passWord)
         {
                   bool Flag = true;
                   Process myproc = new Process();
                   try
                   {
                            myproc.StartInfo.FileName = "cmd.exe";
                            myproc.StartInfo.UseShellExecute = false;
                            myproc.StartInfo.RedirectStandardInput = true;
                            myproc.StartInfo.RedirectStandardOutput = true;
                            myproc.StartInfo.RedirectStandardError = true;
                            myproc.StartInfo.CreateNoWindow = false;
                            myproc.Start();
                            string dosLine = @"net use " + remoteHost + " " + passWord + " " + " /user:" + userName + ">NUL";
                                     myproc.StandardInput.WriteLine(dosLine);
                                     myproc.StandardInput.WriteLine("exit");
                                     while (myproc.HasExited == false)
                                     {
                                               myproc.WaitForExit(1000);
                                     }
                                     string errormsg = myproc.StandardError.ReadToEnd();
                                     if (errormsg != "")
                                     {
                                               Flag = false;
                                     }
                                     myproc.StandardError.Close();
                            }
                            catch (Exception ex)
                            {
                                     Flag = false;
                                     string errmsg = ex.Message;
                            }
                            finally
                            {
                                     try
                                     {
                                               myproc.Close();
                                              myproc.Dispose();
                                     }
                                     catch(Exception ex)
                                     {
                                               Flag = false;
                                               string errmsg = ex.Message;
                                     }
                            }
                            return Flag;
                   }
在页面中引用如下:
usingSystem.IO;
using System.Collections.Specialized;
if(!this.ConnectNet(folder_path,ls_usrid,ls_pswd))
{
DirectoryInfo di = new DirectoryInfo(folder_path);
FileInfo[] fi = di.GetFiles();
NameValueCollection files = new NameValueCollection();
for (int i = 0; i < fi.Length; i++)
{
          File.Copy(folder_path + "//" + fi[i].Name, Server.MapPath("..") + "//" + fi[i].Name, true);
}
}

 Trakback:http://blog.csdn.net/FollowIT/archive/2006/11/09/1375494.aspx

参考:

http://blog.csdn.net/knight94/archive/2006/03/21/631309.aspx

http://blog.csdn.net/knight94/archive/2006/03/31/645367.aspx

原创粉丝点击