zoukankan      html  css  js  c++  java
  • Winform文件上传

    近期在做了一个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         }
  • 相关阅读:
    欧拉函数 & 【POJ】2478 Farey Sequence & 【HDU】2824 The Euler function
    【BZOJ】2982: combination(lucas定理+乘法逆元)
    【vijos】1781 同余方程(拓展欧几里得)
    Disillusioning #1 水题+原题赛(被虐瞎)
    【HDU】3622 Bomb Game(2-SAT)
    小结:双连通分量 & 强连通分量 & 割点 & 割边
    【POJ】2942 Knights of the Round Table(双连通分量)
    【POJ】1523 SPF(割点)
    【POJ】1094 Sorting It All Out(拓扑排序)
    小结:网络流
  • 原文地址:https://www.cnblogs.com/lovejunjuan/p/10750242.html
Copyright © 2011-2022 走看看