• C#实现文件上传到服务器指定地址


    C#实现文件上传到服务器指定地址

    一、建立连接

      public string connectFTP(string vPath, string vUID, string vPassword)
            {
                string errormsg = "";
               Process proc = new Process();
                try
                {
                    proc.StartInfo.FileName = "cmd.exe";
                    proc.StartInfo.UseShellExecute = false;
                    proc.StartInfo.RedirectStandardInput = true;
                    proc.StartInfo.RedirectStandardOutput = true;
                    proc.StartInfo.RedirectStandardError = true;
                    proc.StartInfo.CreateNoWindow = true;
                    proc.Start();
                    string dosLine = "net use " + vPath + " " + vPassword + " /user:" + vUID;
                    proc.StandardInput.WriteLine(dosLine);
                    proc.StandardInput.WriteLine("exit");
                    while (!proc.HasExited)
                    {
                        proc.WaitForExit(1000);
                    }
                    errormsg = proc.StandardError.ReadToEnd();
                    proc.StandardError.Close();
                }
                catch (Exception ex)
                {
                    //throw ex;
                    //MessageBox.Show(ex.Message);
                }
                finally
                {
                    proc.Close();
                    proc.Dispose();
                }
                return errormsg;
            }
    建立连接

    二、上传文件

      public void UploadFile(string vPath, string vUID, string vPassword, string vLocalPath, string file)
            {
                bool status = false;
                status = connectState(vPath, vUID, vPassword);
                if (status)
                {
                    DirectoryInfo theFolder = new DirectoryInfo(vPath + "/" + file);
                    string filename = vLocalPath;
                    Transport(vLocalPath, vPath + "/" + file);
                    //System.Diagnostics.Process.Start(vPath);
                }
                else
                {
                    mesLog.Info("未能连接!");
                    //MessageBox.Show("未能连接!");
                }
            }
    上传文件

    三、连接状态

       public static bool connectState(string vPath, string vUID, string vPassword)
            {
                bool Flag = false;
                Process proc = new Process();
                try
                {
                    proc.StartInfo.FileName = "cmd.exe";
                    proc.StartInfo.UseShellExecute = false;
                    proc.StartInfo.RedirectStandardInput = true;
                    proc.StartInfo.RedirectStandardOutput = true;
                    proc.StartInfo.RedirectStandardError = true;
                    proc.StartInfo.CreateNoWindow = true;
                    proc.Start();
                    string dosLine = "net use " + vPath + " " + vPassword + " /user:" + vUID;
                    proc.StandardInput.WriteLine(dosLine);
                    proc.StandardInput.WriteLine("exit");
                    while (!proc.HasExited)
                    {
                        proc.WaitForExit(1000);
                    }
                    string errormsg = proc.StandardError.ReadToEnd();
                    proc.StandardError.Close();
                    if (string.IsNullOrEmpty(errormsg))
                    {
                        Flag = true;
                    }
                    else
                    {
                        throw new Exception(errormsg);
                    }
                }
                catch (Exception ex)
                {
                    //throw ex;
                    //MessageBox.Show(ex.Message);
                }
                finally
                {
                    proc.Close();
                    proc.Dispose();
                }
                return Flag;
            }
    连接状态

    四、传送

      public static void Transport(string src, string fileName)
            {
                FileStream inFileStream = new FileStream(src, FileMode.Open);
                FileStream outFileStream = new FileStream(fileName, FileMode.OpenOrCreate);
    
                byte[] buf = new byte[inFileStream.Length];
                int byteCount;
    
                while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
                {
                    outFileStream.Write(buf, 0, byteCount);
                }
    
                inFileStream.Flush();
                inFileStream.Close();
                outFileStream.Flush();
                outFileStream.Close();
    
                File.Delete(src);
            }
    传送

     


    技术的发展日新月异,随着时间推移,无法保证本博客所有内容的正确性。如有误导,请大家见谅,欢迎评论区指正!
    我创建了一个.NET开发交流群,用于分享学习心得和讨论相关技术难题。欢迎有兴趣的小伙伴扫码入群,相互学习!

  • 相关阅读:
    IDEA导入springboot项目报错:non-managed pom.xml file found
    Keepalived高可用集群(还没细看)
    Keepalived服务
    *** WARNING
    POJ 2185 Milking Grid (搬运KMP)
    POJ 1961 Period (弱鸡三战KMP)
    POJ 2752 Seek the Name, Seek the Fame (KMP next数组再次应用)
    POJ 2406 Power Strings(KMPnext数组的应用)
    杂模板
    URAL
  • 原文地址:https://www.cnblogs.com/wml-it/p/15188139.html
Copyright © 2020-2023  润新知