C#如何创建文件夹并指定Everyone完全访问权限
最近遇到一个棘手的问题
用C#创建文件夹
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; } }
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; } }
[解决办法]
参考http://hi.baidu.com/tyq_man/blog/item/92d934b450eaddc636d3ca89.html
/* 需要添加以下命名空间: using System.IO; using System.Security.AccessControl; */ string sPath = Server.MapPath(文件夹名称字符串); Directory.CreateDirectory(sPath); addpathPower(sPath, "ASPNET", "FullControl"); ////////////////////////////////////////////////// public void addpathPower(string pathname, string username, string power) { DirectoryInfo dirinfo = new DirectoryInfo(pathname); if ((dirinfo.Attributes & FileAttributes.ReadOnly) != 0) { dirinfo.Attributes = FileAttributes.Normal; } //取得访问控制列表 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; case "Modify": dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Modify, AccessControlType.Allow)); break; } dirinfo.SetAccessControl(dirsecurity); }
[解决办法]