2020-08-21
程序员文章站
2022-05-06 08:01:57
...
C#访问共享文件夹,并下载数据到本地
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DBHelperWMS
{
public class ShareDownload
{
public bool connectState(string path)
{
return connectState(path, "", "");
}
/// <summary>
/// 连接远程共享文件夹
/// </summary>
/// <param name="path">远程共享文件夹的路径</param>
/// <param name="userName">用户名</param>
/// <param name="passWord">密码</param>
/// <returns></returns>
public 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();
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;
}
/// <summary>
/// 从远程服务器下载文件到本地
/// </summary>
/// <param name="src">下载到本地后的文件路径,包含文件的扩展名</param>
/// <param name="dst">远程服务器路径(共享文件夹路径)</param>
/// <param name="fileName">远程服务器(共享文件夹)中的文件名称,包含扩展名</param>
public void TransportRemoteToLocal(string src, string dst, string fileName) //src:下载到本地后的文件路径 dst:远程服务器路径 fileName:远程服务器dst路径下的文件名
{
if (!Directory.Exists(dst))
{
Directory.CreateDirectory(dst);
}
dst = dst + fileName;
FileStream inFileStream = new FileStream(dst, FileMode.Open); //远程服务器文件 此处假定远程服务器共享文件夹下确实包含本文件,否则程序报错
FileStream outFileStream = new FileStream(src, 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();
}
/// <summary>
/// 将本地文件上传到远程服务器共享目录
/// </summary>
/// <param name="src">本地文件的绝对路径,包含扩展名</param>
/// <param name="dst">远程服务器共享文件路径,不包含文件扩展名</param>
/// <param name="fileName">上传到远程服务器后的文件扩展名</param>
public void TransportLocalToRemote(string src, string dst, string fileName) //src
{
FileStream inFileStream = new FileStream(src, FileMode.Open); //此处假定本地文件存在,不然程序会报错
if (!Directory.Exists(dst)) //判断上传到的远程服务器路径是否存在
{
Directory.CreateDirectory(dst);
}
dst = dst + fileName; //上传到远程服务器共享文件夹后文件的绝对路径
FileStream outFileStream = new FileStream(dst, 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();
}
}
}
测试访问代码
public void test()
{
bool status = false;
ShareDownload sd = new ShareDownload();
//连接
status = sd.connectState(@"\\xxx.xx.x.x\e", "administrator", "123456");
if (status)
{
//共享文件夹的目录
DirectoryInfo theFolder = new DirectoryInfo(@"\\xxx.xx.x.x\e\");
string filename = theFolder.ToString();
//执行方法
//TransportLocalToRemote(@"D:\readme1.txt", filename, "readme1.txt"); //实现将本地文件写入到远程服务器
sd.TransportRemoteToLocal(@"D:\123.txt", filename, "123.txt"); //实现将远程服务器文件写入到本地
}
else
{
MessageBox.Show("未能连接!");
}
MessageBox.Show("成功");
}