Winform文件上传

匿名 (未验证) 提交于 2019-12-02 22:06:11

近期在做了一个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         }

 

转载请标明出处:Winform文件上传
文章来源: Winform文件上传
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!