这篇文章介绍了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#把文件上传到服务器中的指定地址
				
        
 
            
        
             猜你喜欢
        
	     - 在C# 8中如何使用默认接口方法详解 2023-03-29
 - user32.dll 函数说明小结 2022-12-26
 - 如何使用C# 捕获进程输出 2023-03-10
 - .NET CORE DI 依赖注入 2023-09-27
 - Unity Shader实现模糊效果 2023-04-27
 - WPF使用DrawingContext实现绘制刻度条 2023-07-04
 - c# 模拟线性回归的示例 2023-03-14
 - Unity3D实现渐变颜色效果 2023-01-16
 - Oracle中for循环的使用方法 2023-07-04
 - C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
 
						
						
						
						
						
				
				
				
				