C# 如何創建一個文件夾

先寫這樣一個類

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.AccessControl;

namespace AssetsCheckDN
{
    public class Folder
    {
        string folderPath = "";

        public string FolderPath
        {
            get { return folderPath; }
            set { folderPath = value; }
        }

        public void createFolder(string path)
        {
            Directory.CreateDirectory(path);
            addpathPower(path, "lucifer-PC", "FullControl");
        }

        public void checkFolder(string path)
        {
            if (Directory.Exists(path))
            {
                addpathPower(path, "lucifer-PC", "FullControl");
                return;
            }
            else
            {
                createFolder(path);
            }
        }

        public void addpathPower(string pathname, string username, string power)
        {
            DirectoryInfo dirinfo = new DirectoryInfo(pathname);
            if ((dirinfo.Attributes & FileAttributes.ReadOnly) != 0)
            { dirinfo.Attributes = FileAttributes.Normal; }
            //C#創建文件夾取得訪問控制列表   
            //DirectorySecurity dirsecurity = dirinfo.GetAccessControl();
            //switch (power)
            //{
            //    case "FullControl": dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
            //        break;
            //    case "ReadOnly": dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Read, AccessControlType.Allow));
            //        break;
            //    case "Write": dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Write, AccessControlType.Allow));
            //        break;
            //    default:
            //        break;
            //}
        }
    }
}

 

調用這個類創建文件夾,並寫文件進入這個文件夾。

 Folder folder = new Folder();

string  backFileName = Application.StartupPath + "\\Data\\" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
 folder.checkFolder(backFileName);
 File.Copy(localDB, backFileName + "
\\CheckAsset.sdf");

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章