日期:2014-05-17 浏览次数:20583 次
if (Ping("192.168.0.144")) //Ping 看通不通 { if (Connect("192.168.0.144", "administrator", "1qaz2wsx???")) //建立连接 { _fileStream = new FileStream(@"\\192.168.0.144\hh\" + _currentFileName, FileMode.OpenOrCreate); } } #region 连接远程机器 public static bool Ping(string remoteHost) { bool Flag = false; Process proc = new Process(); try { proc.StartInfo.FileName = "cmd.exe"; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardInput = true; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.CreateNoWindow = true; proc.Start(); string dosLine = @"ping -n 1 " + remoteHost; proc.StandardInput.WriteLine(dosLine); proc.StandardInput.WriteLine("exit"); while (proc.HasExited == false) { proc.WaitForExit(500); } string pingResult = proc.StandardOutput.ReadToEnd(); if (pingResult.IndexOf("(0% loss)") != -1) { Flag = true; } proc.StandardOutput.Close(); } catch (Exception ex) { } finally { try { proc.Close(); proc.Dispose(); } catch { } } return Flag; } public static bool Connect(string remoteHost, string userName, string passWord) { if (!Ping(remoteHost)) { return false; } bool Flag = true; Process proc = new Process(); try { proc.StartInfo.FileName = "cmd.exe"; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardInput = true; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.CreateNoWindow = true; proc.Start(); string dosLine = @"net use \\" + remoteHost + " " + passWord + " " + " /user:" + userName + ">NUL"; proc.StandardInput.WriteLine(dosLine); proc.StandardInput.WriteLine("exit"); while (proc.HasExited == false) { proc.WaitForExit(1000); } string errormsg = proc.StandardError.ReadToEnd(); if (errormsg != "") { Flag = false; } proc.StandardError.Close(); } catch (Exception ex) { Flag = false; } finally { try { proc.Close(); proc.Dispose(); } catch { } } return Flag; } #endregion 排版差了点 请谅解。 测试通过
------解决方案--------------------
将文件上传到网络共享服务器的方法
1,在文件服务器上,创建一个本地帐户,比如登录名:upload,密码:upload,注意在创建的时候选择“密码永不过期”,去掉勾选“用户下次登录时须更改密码”的选项;
2,在要共享的文件夹上点右键,选择“属性”-“安全”,增加upload帐户可以写入的权限;
3,在要共享的文件夹上点右键,选择“共享”,共享此文件夹,并在“权限”按钮点击后添加帐户upload可修改;
4,在另外一台 Web 服务器上,创建登录名和密码与上面