using System.IO;
//是否存在该路径,不存在就创建该路径
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
//复制文件到指定路径下,eg: string fromUrl = @"E:\test"; string toUrl = @"C:\test";
public static void CopyToLocalFile(string fromUrl, string toUrl)
{
string saveFromUrl = fromUrl;
string saveToUrl = toUrl;
DirectoryInfo dir = new DirectoryInfo(saveFromUrl);
IsExistPath(saveToUrl);
FileSystemInfo[] files = dir.GetFileSystemInfos();
for (int i = 0; i < files.Length; i++)
{
saveFromUrl = fromUrl;
saveToUrl = toUrl;
DirectoryInfo dInfo = files[i] as DirectoryInfo;
if (dInfo != null && ((dInfo.Attributes & FileAttributes.Directory) != 0))
{
saveToUrl += @"\" + dInfo.Name;
saveFromUrl += @"\" + dInfo.Name;
IsExistPath(saveToUrl);
CopyToLocalFile(bk, saveFromUrl, saveToUrl);
}
else
{
saveToUrl += @"\" + files[i].Name;
saveFromUrl += @"\" + files[i].Name;
FileInfo file = files[i] as FileInfo;
file.CopyTo(saveToUrl, true);
}
}
}
//删除该路径下文件
public static void DeleteFile(string url)
{
string saveUrl = url;
DirectoryInfo dir = new DirectoryInfo(saveUrl);
FileSystemInfo[] files = dir.GetFileSystemInfos();
for (int i = 0; i < files.Length; i++)
{
saveUrl = url;
DirectoryInfo dInfo = files[i] as DirectoryInfo;
if (dInfo != null && ((dInfo.Attributes & FileAttributes.Directory) != 0))
{
saveUrl += @"\" + dInfo.Name;
DeleteFile(saveUrl);
}
else
{
saveUrl += @"\" + files[i].Name;
FileInfo file = files[i] as FileInfo;
file.Delete();
}
}
Directory.Delete(url);
}
//创建二进制流文件
{
FileStream filestream;
if (System.IO.Directory.Exists(filePath))
{
filestream = new FileStream(filePath, FileMode.Truncate);
}
else
{
filestream = new FileStream(filePath, FileMode.OpenOrCreate);
}
BinaryWriter br = new BinaryWriter(filestream);
br.Write(data, 0, data.Length);
br.Close();
filestream.Close();
}