方法一
/// <summary>
/// 共享目录上传文件(process cmd命令连接)
/// </summary>
/// <param name="sourceFile">需要上传的文件地址 示例:@"D\hello.txt"</param>
/// <param name="shareFile">共享目录文件夹地址 示例@"\\192.168.1.29\shardata"</param>
/// <param name="user">共享目录用户名</param>
/// <param name="pwd">共享目录密码</param>
/// <param name="errMsg">错误信息</param>
public static void ShareUpload(string sourceFile, string shareFile, string user,string pwd,out string errMsg)
{
string fileName = Path.GetFileName(sourceFile);//获取文件名称
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 " + shareFile + " " + pwd + " /user:" + user;
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))
{
//上传本地文件至共享目录
FileStream inFileStream = new FileStream(sourceFile, FileMode.Open);
FileStream outFileStream = new FileStream(shareDir+"\\"+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(sourceFile);//删除原文件
proc.Close();
proc.Dispose();
}
errMsg = errormsg;
}
catch (Exception ex)
{
errMsg = ex.Message;
proc.Close();
proc.Dispose();
}
}
方法二
注:webClient 方式会出现 操作超时的问题,不推荐使用此方法
/// <summary>
/// 共享目录上传文件(WebClient)
/// </summary>
/// <param name="sourceFile">本地文件路径 示例:@"D:\hello.txt"</param>
/// <param name="shareFile">共享目录文件夹地址 示例:@"\\192.168.1.39\sagrde"</param>
/// <param name="user">共享目录用户名</param>
/// <param name="pwd">共享目录密码</param>
/// <param name="errMsg">错误信息</param>
public static void ShareUpload( string sourceFile,string shareFile, string user, string pwd, out string errMsg)
{
string fileName = Path.GetFileName(sourceFile);//获取文件名称
sring shareFullPath=shareFile+"\\"+fileName;//共享文件目录
try
{
FileStream fs = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
byte[] postArray = r.ReadBytes((int)fs.Length);
WebClient myWebClient = new WebClient();
myWebClient.Credentials = new NetworkCredential(user, pwd);//指定用户名和密码
myWebClient.Credentials = CredentialCache.DefaultCredentials;
Stream postStream = myWebClient.OpenWrite(shareFile, "PUT");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
postStream.Close();
fs.Dispose();
errMsg = "";
}
else
{
errMsg = "共享文件上传没有写入权限";
postStream.Close();
fs.Dispose();
}
}
catch (Exception ex)
{
postStream.Close();
fs.Dispose();
errMsg = ex.Message;
}
}
方法三
/// <summary>
/// 共享目录上传文件(WebRequest)
/// </summary>
/// <param name="sourceFile">需要上传的文件地址 示例:@"D:\hello.txt"</param>
/// <param name="shareFile">共享目录文件夹地址 示例:@"\\192.168.1.39\sagrde"</param>
/// <param name="user">共享目录用户名</param>
/// <param name="pwd">共享目录密码</param>
/// <param name="errMsg">错误信息</param>
public void ShareUploadfile(string sourceFile, string shareFile, string user, string pwd, out string errMsg)
{
string fileName = Path.GetFileName(sourceFile);//获取文件名称
string shareFullFile = shareFile + fileName;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
try
{
FileInfo fileInfo = new FileInfo(sourceFile);
FileStream fs = fileInfo.OpenRead();
var request =(FileWebRequest) WebRequest.Create(new Uri(shareFullFile));
request.Credentials= new NetworkCredential(user,pwd);
request.Method = "POST";
Stream strm = request.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
errMsg = "";
}
catch(Exception ex)
{
errMsg = ex.Message;
}
}