您的位置:首页 > 编程语言 > C#

C# 如何创建一个文件夹

2012-03-28 10:14 337 查看
先写这样一个类

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");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# string path class