C# Create Windows Forms Application Instance 10 [Directory Management]

 Instance 10 already? Don't you create a project yet? Don't know the steps? Can't write code? Please visit the link below to learn!

C# Create Windows Forms Application Instance 1

C# Create Windows Forms Application Instance 2

1. Knowledge points

1. The use of paths:

(1) Absolute path: the path starting from the drive letter    

        " c:\\mydir"    

        "c:\\mydir\\mydir1"    

        @"c:\mydir"

(2) Relative path: is the path starting from the current path    

        a. If the current path is C:\windows          

                ".\system32\cmd.exe " //. indicates the current path    

        b. If the current path is C:\program files        

                 "..\windows\system32\cmd.exe" //.. is the parent directory    

        c. If the current path is C:\program files\common files          

                "..\..\windows\system32\cmd.exe"

Method 1: Determine if the directory exists      

        bool  b1=Directory.Exists("c:\\mydir");  

        或bool  b1=Directory.Exists(@"c:\mydir");

Method 2: Determine whether the directory exists    

        DirectoryInfo  di=new DirectoryInfo("c:\\mydir");    

        bool b2=di.Exists;

Method 1: Create a directory (using the Directory class) 

        Directory.CreateDirectory("c:\\mydir");

Method 2: Create a directory (using DirectoryInfo class)  

        DirectoryInfo  d2=new DirectoryInfo("c:\\mydir");   d2.Create();

Method 1: Delete the directory

        string path1=@”d:\abc”;  if(Directory.Exists(path1))  { Directory.Delete(path1,true); }

Method 2: Delete the directory

        DirectoryInfo di=new DirectoryInfo(@”d:\abd”);  if(di.Exists) {  di.Delete();  }

Method 1: Move the directory

        if(Directory.Exists(@“d:\aaa”)) { Directory.Move(@“d:\aaa”,@“e:\”); }

Method 2: Move the directory

        DirectoryInfo di=new  DirectoryInfo(@“d:\aaa”)); if(di.Exists){   di.MoveTo(@”e:\”); }

Method 1: Get subdirectories

        string  path0=@“c:\”;

        string[] sdirs1=Directory.GetDirectories(path0);

        foreach(string sdir in sdirs1) { Console.WriteLine(“{0}”,sdir); }

Method 2: Get subdirectories

        DirectoryInfo di=new DirectoryInfo(@“c:\”);

        string[] sdirs2=di.GetDirectories();

        foreach(string sdir in sdirs2) {     Console.WriteLine(“{0}”,sdir);    }

Method 1: Get the files in the directory

        string[] fs1=Directory.GetFiles(path1);

        foreach(string f in fs1) { Console.WriteLine(“{0}”,f); }

Method 2: Get the files in the directory

        DirectoryInfo di=new DirectoryInfo(@“c:\”);

        string [] fs1=di.GetFiles();

        foreach(string f in fs1) { Console.WriteLine(“{0}”,f); }

2. Style Design

Select the following  controls in the tool

 

 

 

 

 

3. Code implementation 

using System.Windows.Forms;
using System.IO;

namespace 目录管理
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }
        //”创建”的Click事件的代码
        private void button1_Click(object sender, EventArgs e)
        {
            string cpath = textBox1.Text;
            if (cpath != "")
            {
                if (!Directory.Exists(@cpath))
                { Directory.CreateDirectory(@cpath); }
                else
                { MessageBox.Show("目录已存在或非法目录!"); }
            }
            else
            {
                MessageBox.Show("请输入要创建的目录!");
                this.textBox1.Focus();
            }
        }
        //”删除”的Click事件的代码
        private void button2_Click(object sender, EventArgs e)
        {
            string dpath = textBox2.Text;
            if (dpath != "")
            {
                if (Directory.Exists(@dpath))
                { Directory.Delete(@dpath); }
                else
                { MessageBox.Show("目录不存在或非法目录!"); }
            }
            else
            {
                MessageBox.Show("请输入要删除的目录!");
                this.textBox2.Focus();
            }
        }
        //”移动”的Click事件的代码
        private void button3_Click(object sender, EventArgs e)
        {
            string srcpath = textBox3.Text;
            string destpath = textBox4.Text;
            if (srcpath == "" || destpath == "")
            { MessageBox.Show("请输入要移动的源目录与目的目录路径!"); }
            else
            {
                if (Directory.Exists(@srcpath))
                {
                    if (!Directory.Exists(@destpath))
                    {
                        DirectoryInfo dir = new DirectoryInfo(@srcpath);
                        dir.MoveTo(@destpath);
                    }
                    else
                    { MessageBox.Show("目的目录已存在!"); }
                }
                else
                { MessageBox.Show("源目录不存在!"); }
            }
        }
        //”子目录”的Click事件的代码
        private void button4_Click(object sender, EventArgs e)
        {
            string detailpath = textBox5.Text;
            richTextBox1.Clear();
            if (Directory.Exists(@detailpath))
            {
                string[] dir = Directory.GetDirectories(@detailpath);
                foreach (string adir in dir)
                { richTextBox1.AppendText(adir + '\n'); }
            }
            else
            { MessageBox.Show("目录不存在!"); }
        }
        //”获取文件”的Click事件的代码
        private void button5_Click(object sender, EventArgs e)
        {
            string detailpath = textBox5.Text;
            richTextBox2.Clear();
            if (Directory.Exists(@detailpath))
            {
                string[] files = Directory.GetFiles(@detailpath);
                foreach (string afile in files)
                    richTextBox2.AppendText(afile + '\n');
            }
            else
                MessageBox.Show("目录不存在!");
        }
    }
}

4. Execution effect 

Guess you like

Origin blog.csdn.net/qq_45037155/article/details/124313039