正在开发的项目中有这样的需求,就是web服务器上通过web程序去操作另一台机器上的文件,进行加工并返回值。开始的时候直接用绝对路径进行访问老是访 问不到,一直认为是权限的问题(其实感觉的确是权限的原因),然后把目标文件夹的权限全部放开进行访问,但是还是不可以。根本就访问不到目标路径,所以也 根本不可能操作目标路径下的文件。后来在msdn上找到这段代码,在程序中和目标服务器建立了一个连接,果然好用了。但是按照程序中直接进行net连接却 不好用。不知道为什么?特此也贴出代码来和大家讨论一下:
1public static bool Ping(string remoteHost)
2{
3 bool Flag = false;
4 Process proc = new Process();
5 try
6 {
7 proc.StartInfo.FileName = "cmd.exe";
8 proc.StartInfo.UseShellExecute = false;
9 proc.StartInfo.RedirectStandardInput = true;
10 proc.StartInfo.RedirectStandardOutput = true;
11 proc.StartInfo.RedirectStandardError = true;
12 proc.StartInfo.CreateNoWindow = true;
13 proc.Start();
14 string dosLine = @"ping -n 1 " + remoteHost;
15 proc.StandardInput.WriteLine(dosLine);
16 proc.StandardInput.WriteLine("exit");
17 while (proc.HasExited == false)
18 {
19 proc.WaitForExit(500);
20 }
21 string pingResult = proc.StandardOutput.ReadToEnd();
22 if (pingResult.IndexOf("(0% loss)") != -1)
23 {
24 Flag = true;
25 }
26 proc.StandardOutput.Close();
27 }
28 catch (Exception ex)
29 {
30 //eat it
31 }
32 finally
33 {
34 try
35 {
36 proc.Close();
37 proc.Dispose();
38 }
39 catch
40 {
41 }
42 }
43 return Flag;
44}
45public static bool Connect(string remoteHost, string userName, string passWord)
46{
47 if (!Ping(remoteHost))
48 {
49 return false;
50 }
51 bool Flag = true;
52 Process proc = new Process();
53 try
54 {
55 proc.StartInfo.FileName = "cmd.exe";
56 proc.StartInfo.UseShellExecute = false;
57 proc.StartInfo.RedirectStandardInput = true;
58 proc.StartInfo.RedirectStandardOutput = true;
59 proc.StartInfo.RedirectStandardError = true;
60 proc.StartInfo.CreateNoWindow = true;
61 proc.Start();
62 string dosLine = @"net use \\" + remoteHost + " " + passWord + " " + " /user:" + userName + ">NUL";
63 proc.StandardInput.WriteLine(dosLine);
64 proc.StandardInput.WriteLine("exit");
65 while (proc.HasExited == false)
66 {
67 proc.WaitForExit(1000);
68 }
69 string errormsg = proc.StandardError.ReadToEnd();
70 if (errormsg != "")
71 {
72 Flag = false;
73 }
74 proc.StandardError.Close();
75 }
76 catch (Exception ex)
77 {
78 Flag = false;
79 }
80 finally
81 {
82 try
83 {
84 proc.Close();
85 proc.Dispose();
86 }
87 catch
88 {
89 }
90 }
91 return Flag;
92}
2{
3 bool Flag = false;
4 Process proc = new Process();
5 try
6 {
7 proc.StartInfo.FileName = "cmd.exe";
8 proc.StartInfo.UseShellExecute = false;
9 proc.StartInfo.RedirectStandardInput = true;
10 proc.StartInfo.RedirectStandardOutput = true;
11 proc.StartInfo.RedirectStandardError = true;
12 proc.StartInfo.CreateNoWindow = true;
13 proc.Start();
14 string dosLine = @"ping -n 1 " + remoteHost;
15 proc.StandardInput.WriteLine(dosLine);
16 proc.StandardInput.WriteLine("exit");
17 while (proc.HasExited == false)
18 {
19 proc.WaitForExit(500);
20 }
21 string pingResult = proc.StandardOutput.ReadToEnd();
22 if (pingResult.IndexOf("(0% loss)") != -1)
23 {
24 Flag = true;
25 }
26 proc.StandardOutput.Close();
27 }
28 catch (Exception ex)
29 {
30 //eat it
31 }
32 finally
33 {
34 try
35 {
36 proc.Close();
37 proc.Dispose();
38 }
39 catch
40 {
41 }
42 }
43 return Flag;
44}
45public static bool Connect(string remoteHost, string userName, string passWord)
46{
47 if (!Ping(remoteHost))
48 {
49 return false;
50 }
51 bool Flag = true;
52 Process proc = new Process();
53 try
54 {
55 proc.StartInfo.FileName = "cmd.exe";
56 proc.StartInfo.UseShellExecute = false;
57 proc.StartInfo.RedirectStandardInput = true;
58 proc.StartInfo.RedirectStandardOutput = true;
59 proc.StartInfo.RedirectStandardError = true;
60 proc.StartInfo.CreateNoWindow = true;
61 proc.Start();
62 string dosLine = @"net use \\" + remoteHost + " " + passWord + " " + " /user:" + userName + ">NUL";
63 proc.StandardInput.WriteLine(dosLine);
64 proc.StandardInput.WriteLine("exit");
65 while (proc.HasExited == false)
66 {
67 proc.WaitForExit(1000);
68 }
69 string errormsg = proc.StandardError.ReadToEnd();
70 if (errormsg != "")
71 {
72 Flag = false;
73 }
74 proc.StandardError.Close();
75 }
76 catch (Exception ex)
77 {
78 Flag = false;
79 }
80 finally
81 {
82 try
83 {
84 proc.Close();
85 proc.Dispose();
86 }
87 catch
88 {
89 }
90 }
91 return Flag;
92}