C#把文件上传到服务器中的指定地址

 更新时间:2022年04月23日 11:40:22   作者:農碼一生  
这篇文章介绍了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);
        }

到此这篇关于C#实现文件上传到服务器指定地址的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 浅析C# AsyncLocal如何实现Thread间传值

    浅析C# AsyncLocal如何实现Thread间传值

    这篇文章主要是来和大家一起讨论一下C# AsyncLocal如何实现Thread间传值,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-01-01
  • c# dynamic的使用详解

    c# dynamic的使用详解

    这篇文章主要介绍了c# dynamic的使用详解,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-04-04
  • C#实现一阶卡尔曼滤波算法的示例代码

    C#实现一阶卡尔曼滤波算法的示例代码

    这篇文章主要介绍了C#实现一阶卡尔曼滤波算法的示例代码,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-04-04
  • C#实现获取枚举中元素个数的方法

    C#实现获取枚举中元素个数的方法

    这篇文章主要介绍了C#实现获取枚举中元素个数的方法,是深入理解C#程序设计所需要掌握的基本技巧,需要的朋友可以参考下
    2014-08-08
  • C#小程序15位转18位身份证号代码

    C#小程序15位转18位身份证号代码

    现在我们使用的都是18位身份证号,而以前都是15位身份证号,而如何将15位身份证号转18位身份证号转换为18位身份证号呢?
    2013-02-02
  • C#连接数据库的几种方法

    C#连接数据库的几种方法

    这篇文章介绍了C#连接数据库的几种方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • C#定位txt指定行的方法小例子

    C#定位txt指定行的方法小例子

    近日,在开发CAD插件时需要定位TXT文件指定行并将其选中,在网络找了一下没有找到现成的,自己编了一个定位程序,实现了定位功能..与大家分享
    2013-04-04
  • C#身份证验证小例子

    C#身份证验证小例子

    C#身份证验证小例子,需要的朋友可以参考一下
    2013-04-04
  • C# 注册表 操作实现代码

    C# 注册表 操作实现代码

    Windows 操作系统的注册表包含了很多有关计算机运行的配置方式,打开注册表我们可以看到注册表是按类似于目录的树结构组织的
    2009-07-07
  • C# 中使用 Exceptionless的方法

    C# 中使用 Exceptionless的方法

    这篇文章主要介绍了C# 中使用 Exceptionless的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下
    2020-12-12

最新评论