usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingAspose.Pdf;usingSystem.IO;usingSystem.Diagnostics;namespaceWindowsFormsApplication1
{public partial classForm1 : Form
{publicForm1()
{
InitializeComponent();
}private void button1_Click(objectsender, EventArgs e)
{
UploadECCInvoice_Intranet("");
}///
///内网上传ECC Invoice///
/// ECCInvoice的路径
private void UploadECCInvoice_Intranet(stringpath_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 = newDirectoryInfo(serverFolder);string filename =theFolder.ToString();//执行方法
TransportRemoteToServer(serverFolder+ @"\", destinationFile, "test.pdf"); //实现将远程服务器文件写入到本地
}else{
MessageBox.Show("连接服务器失败!");
}
}///
///从本地上传文件至服务器///
/// 远程服务器路径(共享文件夹路径)
/// 本地文件夹路径
/// 上传至服务器上的文件名,包含扩展名
public static void TransportRemoteToServer(string src, string dst, stringfileName)
{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];intbyteCount;while ((byteCount = outFileStream.Read(buf, 0, buf.Length)) > 0)
{
inFileStream.Write(buf,0, byteCount);
}
inFileStream.Flush();
inFileStream.Close();
outFileStream.Flush();
outFileStream.Close();
}///
///连接远程共享文件夹///
/// 远程共享文件夹的路径
/// 用户名
/// 密码
///
public static bool connectState(string path, string userName, stringpassWord)
{bool Flag = false;
Process proc= newProcess();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 newException(errormsg);
}
}catch(Exception ex)
{throwex;
}finally{
proc.Close();
proc.Dispose();
}returnFlag;
}
}
}