c#将本地文件上传至服务器(内网)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Aspose.Pdf; using System.IO; using System.Diagnostics; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { UploadECCInvoice_Intranet(""); } /// <summary> /// 内网上传ECC Invoice /// </summary> /// <param name="path_ECCInvoice">ECCInvoice的路径</param> private void UploadECCInvoice_Intranet(string path_ECCInvoice) { string destinationFile = @"E:\test\DL180226H012.pdf";//要上传的文件 bool status = false; //连接 string serverFolder = @"\\192.168.1.188\Evan"; string PWD = "qwe123!@#";//kingdee1! status = connectState(serverFolder, "Administrator", PWD); if (status) { //共享文件夹的目录 DirectoryInfo theFolder = new DirectoryInfo(serverFolder); string filename = theFolder.ToString(); //执行方法 TransportRemoteToServer(serverFolder + @"\", destinationFile, "test.pdf"); //实现将远程服务器文件写入到本地 } else { MessageBox.Show("连接服务器失败!"); } } /// <summary> /// 从本地上传文件至服务器 /// </summary> /// <param name="src">远程服务器路径(共享文件夹路径)</param> /// <param name="dst">本地文件夹路径</param> /// <param name="fileName">上传至服务器上的文件名,包含扩展名</param> public static void TransportRemoteToServer(string src, string dst, string fileName) { if (!Directory.Exists(dst)) { Directory.CreateDirectory(dst); } src = src + fileName; FileStream inFileStream = new FileStream(src, FileMode.OpenOrCreate); //从远程服务器下载到本地的文件 FileStream outFileStream = new FileStream(dst, FileMode.Open); //远程服务器文件 此处假定远程服务器共享文件夹下确实包含本文件,否则程序报错 byte[] buf = new byte[outFileStream.Length]; int byteCount; while ((byteCount = outFileStream.Read(buf, 0, buf.Length)) > 0) { inFileStream.Write(buf, 0, byteCount); } inFileStream.Flush(); inFileStream.Close(); outFileStream.Flush(); outFileStream.Close(); } /// <summary> /// 连接远程共享文件夹 /// </summary> /// <param name="path">远程共享文件夹的路径</param> /// <param name="userName">用户名</param> /// <param name="passWord">密码</param> /// <returns></returns> public static bool connectState(string path, string userName, string passWord) { 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(); proc.StandardInput.WriteLine("net use * /del /y"); string dosLine = "net use " + path + " " + passWord + " /user:" + userName; 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; } finally { proc.Close(); proc.Dispose(); } return Flag; } } }
一个Demo,将本地的这个E:\test\DL180226H012.pdf文件,上传至\\192.168.1.188\Evan路径下。
版权声明:本文为evanmemo原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。