当前位置: 移动技术网 > IT编程>开发语言>.net > C# 读写网上邻居中的共享文件

C# 读写网上邻居中的共享文件

2018年11月21日  | 移动技术网IT编程  | 我要评论

男童被输过期盐水,cctv2对话,南阳天气预报查询

读写网上邻居共享的文件夹,和操作本地文件夹类似,只要有权限读取写入即可。

分为以下2步:

1.打通共享文件夹权限

2.操作文件

 

 

打通共享文件夹权限

 1         /// <summary>
 2         /// 连接共享文件
 3         /// </summary>
 4         /// <param name="path">共享文件地址</param>
 5         /// <param name="username">用户名</param>
 6         /// <param name="password">密码</param>
 7         /// <returns>true:连接成功 false:连接失败</returns>
 8         public static bool connectstate(string path, string username, string password)
 9         {
10             bool flag = false;
11             process proc = new process();
12             try
13             {
14                 proc.startinfo.filename = "cmd.exe";
15                 proc.startinfo.useshellexecute = false;
16                 proc.startinfo.redirectstandardinput = true;
17                 proc.startinfo.redirectstandardoutput = true;
18                 proc.startinfo.redirectstandarderror = true;
19                 proc.startinfo.createnowindow = true;
20                 proc.start();
21                 string dosline = @"net use " + path + " /user:" + username + " " + password + " /persistent:yes";
22                 proc.standardinput.writeline(dosline);
23                 proc.standardinput.writeline("exit");
24                 while (!proc.hasexited)
25                 {
26                     proc.waitforexit(1000);
27                 }
28                 string errormsg = proc.standarderror.readtoend();
29                 proc.standarderror.close();
30                 if (string.isnullorempty(errormsg))
31                 {
32                     flag = true;
33                 }
34                 else
35                 {
36                     throw new exception(errormsg);
37                 }
38             }
39             catch (exception ex)
40             {
41                 throw ex;
42             }
43             finally
44             {
45                 proc.close();
46                 proc.dispose();
47             }
48 
49             return flag;
50         }
view code

 

创建文件夹

 

1 directoryinfo dirinfo = new directoryinfo("\\win-r3377jmr1lg\sharefolder");
2 if (dirinfo.exists == false)
3 {
4     dirinfo.create();
5 }
view code

 

 

 

 

上传文件

 1         /// <summary>
 2         /// 上传文件到共享文件夹
 3         /// </summary>
 4         /// <param name="sourcefile">本地文件</param>
 5         /// <param name="remotefile">远程文件</param>
 6         public static void uploadfile(string sourcefile, string remotefile)
 7         {
 8             //判断文件夹是否存在 ->不存在则创建
 9             var targetfolder = path.getdirectoryname(remotefile);
10             directoryinfo thefolder = new directoryinfo(targetfolder);
11             if (thefolder.exists == false)
12             {
13                 thefolder.create();
14             }
15 
16             try
17             {
18                 webclient mywebclient = new webclient();
19                 networkcredential cread = new networkcredential();
20                 mywebclient.credentials = cread;
21 
22                 using (filestream fs = new filestream(sourcefile, filemode.open, fileaccess.read))
23                 {
24                     using (binaryreader r = new binaryreader(fs))
25                     {
26                         byte[] postarray = r.readbytes((int)fs.length);
27                         using (stream poststream = mywebclient.openwrite(remotefile))
28                         {
29                             if (poststream.canwrite == false)
30                             {
31                                 logutil.error($"{remotefile} 文件不允许写入~");
32                                 return;
33                             }
34 
35                             poststream.write(postarray, 0, postarray.length);
36                         }
37                     }
38                 }
39             }
40             catch (exception ex)
41             {
42                 string errmsg = $"{remotefile}  ex:{ex.tostring()}";
43                 logutil.error(errmsg);
44                 console.writeline(errmsg);
45             }
46         }
view code

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网