⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program.cs

📁 用CSharp 删除文件和文件夹,希望对大家有一个帮助
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
namespace DeleteFile
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {

                XmlDocument doc = new XmlDocument();
                string strXmlFilePath = Application.StartupPath + "\\DeleteFile.xml";
                doc.Load(strXmlFilePath);
                XmlNode root = doc.FirstChild;
                XmlNodeList nodelist = root.SelectNodes("file");
                for (int i = 0; i < nodelist.Count; i++)
                {

                    XmlNode nodetemp = nodelist.Item(i);

                    string FilePath = Application.StartupPath + nodetemp.Attributes["path"].Value;


                    if (Directory.Exists(FilePath))
                    {



                        //DirectoryInfo di = new DirectoryInfo(FilePath);
                        //di.Attributes = System.IO.FileAttributes.Normal;
                        //di.Delete(true);
                        //Directory.Delete( FilePath, true);
                        DeleteFolder(FilePath);
                    }
                    else
                    {


                        FileInfo fi = new FileInfo(FilePath);
                        if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                            fi.Attributes = FileAttributes.Normal;
                        File.Delete(FilePath);
                    }
                }
                //Console.WriteLine("删除成功");
                //XmlNodeList nodelist2 = root.SelectNodes("startup");
                //XmlNode nodestartup = nodelist2.Item(0);
                //string strFilePath = Application.StartupPath + "\\" + nodestartup.Attributes["path"].Value;
                //Process.Start(strFilePath);
                //Application.Exit();
            }
            catch (Exception e)
            {
               
                
            }
            finally
            {
                XmlDocument doc = new XmlDocument();
                string strXmlFilePath = Application.StartupPath + "\\DeleteFile.xml";
                doc.Load(strXmlFilePath);
                XmlNode root = doc.FirstChild;
                XmlNodeList nodelist2 = root.SelectNodes("startup");
                for (int i = 0; i < nodelist2.Count; i++)
                {
                    string startPath = nodelist2[i].Attributes["path"].Value;
                    string temp = startPath.Replace(".exe","");
                     System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcessesByName(temp);
                     if (myProcesses.Length > 1)
                     {
                         continue;
                     }
                     else
                     {
                         Process.Start(startPath);
                     }
                }
                //XmlNode nodestartup = nodelist2.Item(0);
                //string strFilePath = Application.StartupPath + "\\" + nodestartup.Attributes["path"].Value;
                //Process.Start(strFilePath);
                
                Application.Exit();
            }
            
        }
        public static void DeleteFolder(string dir)
        {
            foreach (string d in Directory.GetFileSystemEntries(dir))
            {
                if (File.Exists(d))
                {
                    FileInfo fi = new FileInfo(d);
                    if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                        fi.Attributes = FileAttributes.Normal;
                    File.Delete(d);//直接删除其中的文件   
                }
                else
                    DeleteFolder(d);//递归删除子文件夹   
            }
            Directory.Delete(dir);//删除已空文件夹   
        }  
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -