首页 > 代码库 > C#操作文件、文件夹(复制、删除、新增、设置只读)-技术&分享
C#操作文件、文件夹(复制、删除、新增、设置只读)-技术&分享
C#操作文件、文件夹(复制、删除、新增、设置只读)-技术&分享
using System; using System.IO; using System.Windows; using System.Windows.Documents; using System.Xml; namespace WorkItemCreateBussiness.HzClass { public class HzFile { /// <summary> /// 删除指定文件夹下的所有文件和子文件夹 /// </summary> /// <param name="fileFolder">指定删除的文件夹路径</param> /// <param name="isDeleteCurrentFolder">是否删除指定文件夹,true:删除;false:不删除</param> /// <returns>成功返回true,失败返回false</returns> public bool DeleteFolder(string fileFolder,bool isDeleteCurrentFolder) { try { if (Directory.Exists(fileFolder)) { foreach (var file in Directory.GetFiles(fileFolder, "*.*", SearchOption.AllDirectories)) { File.SetAttributes(file, System.IO.FileAttributes.Normal); File.Delete(file); } Directory.Delete(fileFolder, true); if (!isDeleteCurrentFolder) { Directory.CreateDirectory(fileFolder); } } } catch (Exception ex) { MessageBox.Show(ex.Message); return false; } return true; } /// <summary> /// Hzfile /// 获取指定文件的扩展名 /// </summary> /// <param name="filePath">指定文件的文件夹绝对路径</param> /// <returns>返回文件的扩展名,如果文件扩展名为空或文件路径错误则返回空</returns> public string GetFileExtension(string filePath) { string filePathExtension = ""; try { if (Directory.Exists(filePath)) { filePathExtension = filePath.Substring(filePath.LastIndexOf(".") + 1); } } catch (Exception ex) { MessageBox.Show(ex.Message); return ""; } return filePathExtension; } public void CopyBaseSql(string fromFolder, string toFolder) //拷贝sql目录下的标准sql文件到\Upgrade\BaseCode\Code目录下 { try { string shortFolderName; if (toFolder.IndexOf("相关文档") > -1) { if (Directory.Exists(toFolder)) { Directory.Delete(toFolder, true); } Directory.CreateDirectory(toFolder); } //File.SetAttributes(to,FileAttributes.Hidden); // 子文件夹 foreach (string sub in Directory.GetDirectories(fromFolder)) { shortFolderName = sub.Substring(sub.LastIndexOf("\\") + 1, sub.Length - sub.LastIndexOf("\\") - 1); CopyBaseSql(sub + "\\", toFolder + "\\" + Path.GetFileName(sub)); } //文件 foreach (string file in Directory.GetFiles(fromFolder)) { File.Copy(file, toFolder + "\\" + Path.GetFileName(file), true); } } catch { //MessageBox.Show("创建sql文件失败"); } } /// <summary> /// Hzfile /// 拷贝fromFolder文件夹所有的子目录和文件到toFolder文件夹下 /// </summary> /// <param name="fromFolder">需要拷贝的文件夹</param> /// <param name="toFolder">拷贝到的文件夹的路径</param> /// <returns>如果存在异常返回true,否则返回false</returns> public bool CopyFolder(string fromFolder, string toFolder) { try { if (Directory.Exists(toFolder)) { Directory.Delete(toFolder, true); } Directory.CreateDirectory(toFolder); // 子文件夹 foreach (string sub in Directory.GetDirectories(fromFolder)) { //shortFolderName = sub.Substring(sub.LastIndexOf("\\") + 1, sub.Length - sub.LastIndexOf("\\") - 1); CopyFolder(sub + "\\", toFolder + "\\" + Path.GetFileName(sub)); } //文件 foreach (string file in Directory.GetFiles(fromFolder)) { File.Copy(file, toFolder + "\\" + Path.GetFileName(file), true); } } catch(Exception ex) { //MessageBox.Show("创建sql文件失败"); return false; } return true; } /// <summary> /// Hzfile /// 拷贝fromFolder文件夹到toFolder文件夹下 /// </summary> /// <param name="fromFolder">需要拷贝的文件夹</param> /// <param name="toFolder">拷贝到的文件夹的路径</param> /// <returns>如果存在异常返回true,否则返回false</returns> public bool CopyFileToCommonFolder(string fromFolder, string toCommonFolder) { try { if (Directory.Exists(toCommonFolder)) { Directory.Delete(toCommonFolder, true); } Directory.CreateDirectory(toCommonFolder); //文件 foreach (string file in Directory.GetFiles(fromFolder,"*.*",SearchOption.AllDirectories)) { File.Copy(file, toCommonFolder + "\\" + Path.GetFileName(file), true); } } catch (Exception ex) { //MessageBox.Show("创建sql文件失败"); return false; } return true; } /// <summary> /// Hzfile /// 拷贝fromFolder文件夹到toFolder文件夹下, /// </summary> /// <param name="fromFolder">需要拷贝的文件夹</param> /// <param name="toFolder">拷贝到的文件夹的路径</param> /// <returns>如果存在异常返回true,否则返回false</returns> public bool CopyFileToCommonFolder(string fromFolder, string toCommonFolder,XmlDocument xmlDoc,string xPath) { try { if (Directory.Exists(toCommonFolder)) { Directory.Delete(toCommonFolder, true); } Directory.CreateDirectory(toCommonFolder); //文件 foreach (string file in Directory.GetFiles(fromFolder, "*.*", SearchOption.AllDirectories)) { string fileFullName = Path.GetFullPath(file); string fileName = Path.GetFileName(file); string folder=fileFullName.Substring(fromFolder.Length, fileFullName.Length - fromFolder.Length - fileName.Length - 1); XmlNodeList fileNode = xmlDoc.SelectNodes(@"Mysoft.Data/UpgradeData/Package/Code/File[@fileName=" + "‘" + fileName + "‘and @folder=" + "‘" + folder + "‘" + "]"); if (fileNode.Count == 1) { string updateFileName = fileNode.Item(0).SelectSingleNode("FileContent").Attributes["fileName"].Value; File.Copy(file, toCommonFolder + "\\" + updateFileName, true); } //string fileName = FileNode.ChildNodes[0].Attributes["fileName"].Value; //File.Copy(file, toCommonFolder + "\\" + fileName, true); } } catch (Exception ex) { //MessageBox.Show("创建sql文件失败"); return false; } return true; } /// <summary> /// Hzfile /// 判断rightFolder种的一个文件在leftFolder中是否存在 /// </summary> /// <param name="leftFolder">修复前文件夹</param> /// <param name="rightFolder">修复后文件夹</param> /// <param name="rightFile">需要确认是否存在的文件rightFile</param> /// <returns>右侧文件夹map路径下文件rightFile,在左边文件夹map目录下也存在,存在则返回edit,否则返回add</returns> public string DiffFileExit(string leftFolder, string rightFolder,string rightFile) { try { if (Directory.Exists(leftFolder) && Directory.Exists(rightFolder)) { //if (File.Exists(leftFolder.Substring(0, rightFile.IndexOf("after")+6) +rightFile.Substring(rightFile.IndexOf("after")+5))) if (File.Exists(leftFolder + rightFile)) { return "edit"; } else { return "add"; } } else { return ""; } } catch (Exception ex) { //MessageBox.Show(ex.Message); return ""; } return ""; } /// <summary> /// 文件夹、子文件夹、文件去只读 /// </summary> /// <param name="dirPath">指定去只读的文件夹路径</param> /// <returns>成功返回true,失败返回false</returns> public bool SetNotReadOnly(string dirPath) { try { string[] dirPathes = Directory.GetDirectories(dirPath, "*.*", SearchOption.AllDirectories); foreach (var dp in dirPathes) { if (!Directory.Exists(dp)) { continue; } DirectoryInfo dir = new DirectoryInfo(dp); dir.Attributes = FileAttributes.Normal & FileAttributes.Directory; string[] filePathes = Directory.GetFiles(dp, "*.*", SearchOption.AllDirectories); foreach (var fp in filePathes) { File.SetAttributes(fp, FileAttributes.Normal); } } } catch (Exception ex) { MessageBox.Show(ex.Message); return false; } return true; } } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。