1、工具制作原因

        个人在工作经历中,需要频繁建立文件夹,就设计一个配置文件夹建立工具,为了方便平时自己快速建立文件夹。然后后期方便自己能够再增加其他管控文件夹,并且使用自己的工具集合在一起,方便使用。

2、实现UI

3、配置文件

使用ini文件配置你需要批量生成的文件夹,并且做到可以配置修改。

4、实现代码
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace CANoeHelperTool
{
    public partial class FileBatchCreationAccordingToRules : Form
    {
        public FileBatchCreationAccordingToRules()
        {
            InitializeComponent();
            iniFile = new IniFile();
        }

        private string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
        private IniFile iniFile;
        private List<string> ListPathFile;


        private void bt_ReadIni_Click(object sender, EventArgs e)
        {
            ListPathFile = new List<string>();
            iniFile.LoadFile(baseDirectory + textBox_FileIni.Text);
            GetCANoeTemplate();
        }

        private void GetCANoeTemplate()
        {
            textBox_FileName.Text = iniFile.GetValue(comboBox_ReadIni.Text, "FileName");
            ListPathFile = GetListPath(comboBox_ReadIni.Text, "File");
            if (ListPathFile == null)
            {
                return;
            }
            listBoxFilePath.Items.Clear();
            foreach (string item in ListPathFile)
            {
                listBoxFilePath.Items.Add(item);
            }
        }


        private void bt_OutputPath_Click(object sender, EventArgs e)
        {
            listBoxLog.Items.Clear();
            if (NewFile(textBox_OutFilepath.Text + "\\" + textBox_FileName.Text, ListPathFile))
            {
                listBoxLog.Items.Add(textBox_OutFilepath.Text + "\\" + textBox_FileName.Text + "   文件夹输出成功");
            }
            else
            {
                listBoxLog.Items.Add(textBox_OutFilepath.Text + "\\" + textBox_FileName.Text + "   文件夹输出失败");
            }
        }

        private bool NewFile(string basePath, List<string> ListPathFile)
        {
            try
            {
                foreach (string folderName in ListPathFile)
                {
                    string folderPath = basePath + folderName;

                    // 检查文件夹是否已经存在
                    if (!Directory.Exists(folderPath))
                    {
                        // 如果不存在,则创建新的文件夹
                        Directory.CreateDirectory(folderPath);
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        private void bt_Open_Click(object sender, EventArgs e)
        {
            OpenFolder(textBox_OutFilepath.Text + "\\" + textBox_FileName.Text);
        }

        private void OpenFolder(string folderPath)
        {
            try
            {
                Process.Start(folderPath);
                listBoxLog.Items.Add("OpenFolder OK");
            }
            catch (Exception ex)
            {
                listBoxLog.Items.Add("An error occurred: " + ex.Message);
            }
        }
5、后记

        简单分享希望对你有用。

Logo

获取更多汽车电子技术干货

更多推荐