程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#操作文件、文件夾(復制、刪除、新增、設置只讀)-技術&分享

C#操作文件、文件夾(復制、刪除、新增、設置只讀)-技術&分享

編輯:C#入門知識

C#操作文件、文件夾(復制、刪除、新增、設置只讀)-技術&分享

using System;
using System.IO;
using System.Windows;
using System.Windows.Documents;
using System.Xml;

namespace WorkItemCreateBussiness.HzClass
{
    public class HzFile
    {
        /// 
        /// 刪除指定文件夾下的所有文件和子文件夾
        /// 
        /// 指定刪除的文件夾路徑
        /// 是否刪除指定文件夾,true:刪除;false:不刪除
        /// 成功返回true,失敗返回false
        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;

        }

        /// 
        /// Hzfile
        /// 獲取指定文件的擴展名
        /// 
        /// 指定文件的文件夾絕對路徑
        /// 返回文件的擴展名,如果文件擴展名為空或文件路徑錯誤則返回空
        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文件失敗");
            }
        }

        /// 
        /// Hzfile
        /// 拷貝fromFolder文件夾所有的子目錄和文件到toFolder文件夾下
        /// 
        /// 需要拷貝的文件夾
        /// 拷貝到的文件夾的路徑
        /// 如果存在異常返回true,否則返回false
        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;
        }


        /// 
        /// Hzfile
        /// 拷貝fromFolder文件夾到toFolder文件夾下
        /// 
        /// 需要拷貝的文件夾
        /// 拷貝到的文件夾的路徑
        /// 如果存在異常返回true,否則返回false
        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;
        }

        /// 
        /// Hzfile
        /// 拷貝fromFolder文件夾到toFolder文件夾下,
        /// 
        /// 需要拷貝的文件夾
        /// 拷貝到的文件夾的路徑
        /// 如果存在異常返回true,否則返回false
        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;
        }


        /// 
        /// Hzfile
        /// 判斷rightFolder種的一個文件在leftFolder中是否存在
        /// 
        /// 修復前文件夾
        /// 修復後文件夾
        /// 需要確認是否存在的文件rightFile
        /// 右側文件夾map路徑下文件rightFile,在左邊文件夾map目錄下也存在,存在則返回edit,否則返回add
        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 "";

        }


        /// 
        /// 文件夾、子文件夾、文件去只讀
        /// 
        /// 指定去只讀的文件夾路徑
        /// 成功返回true,失敗返回false
        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;
        }
    }
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved