用FileStream实现复制文件的几种方式
2020-04-28 17:40 zjse 阅读(513) 评论(0) 收藏 举报1.从一台服务器复制到另一台服务器(通过建共享文件夹)
public void OneToOne(string[] args)
{
var myWebClient = new WebClient();
//源服务器用户名密码
var cread = new NetworkCredential("administrator", "123456", "domain");
myWebClient.Credentials = cread;
//源服务器文件的地址(选中共享文件夹右键属性,可以看到机器名)
var fs = new FileStream(@"\\机器名\共享文件夹的名称\水果\苹果\20200408155541.xls", FileMode.Open, FileAccess.Read);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
//目标服务器 文件存放地址
FileStream fwrite = new FileStream(@"D:\目标水果\20200408155541.xls", FileMode.Append);
fwrite.Write(bytes, 0, bytes.Length);
}
## 将文件从服务器某个文件夹移到另一个文件夹
/// <summary>
/// 从一个文件夹复制到另一个文件夹
/// </summary>
public void MoveFile()
{
string Frompath = @"D:\Test\OutPut";
string directoryPath = @"D:\report";
string[] pdfList = Directory.GetFiles(Frompath, "*.pdf"); //PDF文件
foreach (string f in pdfList)
{
string fName = f.Substring(Frompath.Length + 1);
System.IO.File.Copy(System.IO.Path.Combine(Frompath, fName), System.IO.Path.Combine(directoryPath, fName));
}
}
## 将某些内容存到某个文件夹
public void SaveToFile()
{
string txt = "你要保存的内容"; // 你可以修改双引号里面的内容
string filename = "temp.txt"; // temp.txt 文件名你可以任意修改
System.IO.StreamWriter sw = new System.IO.StreamWriter(filename);
sw.Write(txt); // 将内容保存到文件
sw.Close();
}
浙公网安备 33010602011771号