当前位置: 移动技术网 > IT编程>开发语言>.net > Winform文件上传

Winform文件上传

2019年04月23日  | 移动技术网IT编程  | 我要评论

骑马与砍杀战团序列号,百菲,辛子陵是什么人

近期在做了一个winform的项目的附件上传的需求

最初项目选型的时候有三种

1.使用webservice、webapi上传文件

2,直接保存在数据库中

3.使用共享目录+dos命令

 

第一种有文件大小限制、设计到的知识比较多,第二种会给数据库增加不小的压力,于是最后选了第三种

下面上关键代码,代码很简单,每个函数都写了说明

 1         /// <summary>
 2         /// 连接远程目录
 3         /// </summary>
 4         /// <param name="path"></param>
 5         /// <param name="username"></param>
 6         /// <param name="password"></param>
 7         /// <returns></returns>
 8         public static bool connectstate(string path=@"\\192.168.0.136\软件", string username= "administrator", 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 dosline1 = "net use * /del /y";
22                 string dosline2 = "net use " + path + " " + password + " /user:" + username;
23                 //proc.standardinput.writeline(dosline1);
24                 proc.standardinput.writeline(dosline2);
25                 proc.standardinput.writeline("exit");
26                 while (!proc.hasexited)
27                 {
28                     proc.waitforexit(1000);
29                 }
30                 string errormsg = proc.standarderror.readtoend();
31                 proc.standarderror.close();
32                 if (string.isnullorempty(errormsg))
33                 {
34                     flag = true;
35                 }
36                 else
37                 {
38                     //throw new exception(errormsg);
39                 }
40             }
41             catch (exception ex)
42             {
43                 //throw ex;
44                 proc.close();
45                 proc.dispose();
46             }
47             finally
48             {
49                 proc.close();
50                 proc.dispose();
51             }
52             return flag;
53         }
54         /// <summary>
55         /// 向远程文件夹保存本地内容,或者从远程文件夹下载文件到本地
56         /// </summary>
57         /// <param name="src">要保存的文件的路径,如果保存文件到共享文件夹,这个路径就是本地文件路径如:@"d:\1.avi"</param>
58         /// <param name="dst">保存文件的路径,不含名称及扩展名</param>
59         /// <param name="filename">保存文件的名称以及扩展名</param>
60         public static void transport(string src, string dst, string filename)
61         {
62 
63             filestream infilestream = new filestream(src, filemode.open);
64             if (!directory.exists(dst))
65             {
66                 directory.createdirectory(dst);
67             }
68             dst = dst + filename;
69             filestream outfilestream = new filestream(dst, filemode.openorcreate);
70 
71             byte[] buf = new byte[infilestream.length];
72 
73             int bytecount;
74 
75             while ((bytecount = infilestream.read(buf, 0, buf.length)) > 0)
76             {
77                 outfilestream.write(buf, 0, bytecount);
78             }
79 
80             infilestream.flush();
81             infilestream.close();
82             outfilestream.flush();
83             outfilestream.close();
84 
85         }
86         /// <summary>
87         /// 避免保存到服务器上的文件有重复,使用guid对文件进行重命名
88         /// </summary>
89         public static string getguid
90         {
91             get
92             {
93                 return guid.newguid().tostring().tolower();
94             }
95         }

 

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

相关文章:

验证码:
移动技术网