大数据量文本通过Net use方式可以实现文件传输:
public class UpfileHelper { /// <summary> /// 内网上传ECC Invoice /// </summary> /// <param name="path_ECCInvoice">ECCInvoice的路径</param> public static void UploadECCInvoice_Intranet(string path_ECCInvoice) { string destinationFile = @"C:\tempFile\serviceDetails.json";//要上传的文件 bool status = false; //连接 / string serverFolder = @"\user-PCsharefile";//用户文件夹子 string PWD = "password"; status = connectState(serverFolder, "Administrator", PWD);//Administrator 服务器登录 if (status) { //共享文件夹的目录 DirectoryInfo theFolder = new DirectoryInfo(serverFolder); string filename = theFolder.ToString(); //执行方法 TransportRemoteToServer(serverFolder + @"", destinationFile, "serviceDetails.json"); //实现将远程服务器文件写入到本地 } 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; } }