日期:2014-05-18  浏览次数:21443 次

C#如何创建文件夹并指定Everyone完全访问权限
最近遇到一个棘手的问题

用C#创建文件夹
C# code

public bool WriteOperationLog(string category, string msg)
        {
            try
            {
                string dir = string.Empty;
                string path = string.Empty;
                if (string.IsNullOrEmpty(m_log_path) || string.IsNullOrEmpty(m_log_file_name))
                {
                    error_msg = "No log file name or log file path.";
                    return false;
                }
                if (Path.IsPathRooted(m_log_path))
                {
                    dir = m_log_path;
                }
                else
                {
                    dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, m_log_path);
                }
                Directory.CreateDirectory(dir);   //创建文件夹
                path = Path.Combine(dir, m_log_file_name + ".txt");
                FileStream f;
                f = new FileStream(dir, FileMode.Append);
                StreamWriter r = new StreamWriter(f);
                r.WriteLine(string.Format("{0}t{1}t{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), category, msg));
                r.Flush();
                r.Close();
                BackupLog();
                DeleteLog();
                return true;
            }
            catch (Exception ex)
            {
                error_msg = ex.ToString();
                return false;
            }
        }



调用这个函数会报 “对路径“H:\Charley\MyApp\XJFIR\XJFIRClient\bin\Debug\log”的访问被拒绝”。

注:这个函数和调用程序不在同一个程序集。

然后,我想可能是没有权限创建目录,于是我想创建文件夹时加入Everyone的完全访问权限,从网上搜了一段代码,修改这个函数如下:
C# code

public bool WriteOperationLog(string category, string msg)
        {
            try
            {
                string dir = string.Empty;
                string path = string.Empty;
                if (string.IsNullOrEmpty(m_log_path) || string.IsNullOrEmpty(m_log_file_name))
                {
                    error_msg = "No log file name or log file path.";
                    return false;
                }
                if (Path.IsPathRooted(m_log_path))
                {
                    dir = m_log_path;
                }
                else
                {
                    dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, m_log_path);
                }
                Directory.CreateDirectory(dir);
                DirectoryInfo dir_info = new DirectoryInfo(dir);
                DirectorySecurity dir_security = new DirectorySecurity();
                dir_security.AddAccessRule(new FileSystemAccessRule("Everyone ", FileSystemRights.WriteData, AccessControlType.Allow));
                dir_info.SetAccessControl(dir_security);
                path = Path.Combine(dir, m_log_file_name + ".txt");
                FileStream f;
                f = new FileStream(dir, FileMode.Append);
                StreamWriter r = new StreamWriter(f);
                r.WriteLine(string.Format("{0}t{1}t{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), category, msg));
                r.Flush();
                r.Close();
                BackupLog();
                DeleteLog();
                return true;
            }
            catch (Exception ex)
            {
                error_msg = ex.ToString();
                return false;
            }
        }



可是运行又出现问题了:执行到 dir_security.AddAccessRule(new FileSystemAccessRule("Everyone ", FileSystemRights.WriteData, AccessControlType.Allow));
报:“此工作站和主域间的信任关系失败”。

实在没辙了,不曾想用C#创建一个目录还这么难,请高手指教,不胜感谢!!!