C# Winform开发 文件/文件夹的新建

ControlFileClass类中的CreateFolder和CreateFile函数:

/// 
        /// 创建文件夹
        /// 
        /// 文件夹路径
        /// 文件夹名
        public static void CreateFolder(string dirPath, string name)
        {
            foreach (string d in Directory.GetFileSystemEntries(dirPath))
            {
                if (File.Exists(dirPath + @"\" + name))
                {
                    Console.WriteLine("创建文件夹 " + name + " 失败,文件夹已经存在");
                    return;
                }
            }//end of for
            DirectoryInfo info = new DirectoryInfo(dirPath);
            info.CreateSubdirectory(name);
            //info.Parent.CreateSubdirectory(name);//可以在父目录生成文件夹,很方便

        }//end of CreateFolder

        /// 
        /// 创建文件
        /// 
        /// 文件路径
        /// 文件名
        public static void CreateFile(string dirPath, string name)
        {
            foreach (string d in Directory.GetFileSystemEntries(dirPath))
            {
                if (File.Exists(dirPath + @"\" + name))
                {
                    Console.WriteLine("创建文件 " + name + " 失败,文件已经存在");
                    return;
                }
            }//end of for
            File.Create(dirPath + @"\" + name);
        }//end of CreateFile

 

简单的实例:

ControlFileClass.CreateFolder(rootPath,"wybnmsl");
ControlFileClass.CreateFile(rootPath,"2.txt");

 

你可能感兴趣的:(C#,winform,c#,winform)