C#如何创建文件夹并指定Everyone完全访问权限

仙剑 2012-05-20 12:04:16
最近遇到一个棘手的问题

用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;
}
}


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

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

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

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#创建一个目录还这么难,请高手指教,不胜感谢!!!

...全文
903 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
仙剑 2012-05-21
  • 打赏
  • 举报
回复
非常感谢各位的回复,只是有一点不太明白

以前函数和调用程序在一个程序集里,没有遇到访问权限的问题,因为是使用运行程序的用户身份创建的文件夹,现在把函数放到一个独立的程序集,怎么就出现了访问权限的问题。
iyomumx 2012-05-20
  • 打赏
  • 举报
回复
指定用户名的时候,应该是用"计算机或域名\用户名"
  • 打赏
  • 举报
回复
创建一个目录?

目录已经存在了,还能再次创建?
iyomumx 2012-05-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

引用 2 楼 的回复:

指定用户名的时候,应该是用"计算机或域名\用户名"


那我发布程序后,不再这个域中运行,怎么办呢
[/Quote]

用System.Environment.UserName属性和System.Environment.UserDomainName获取当前用户名和域名/主机名
蝶恋花雨 2012-05-20
  • 打赏
  • 举报
回复
参考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);
}

http://www.cnblogs.com/top5/archive/2010/04/12/1710141.html
仙剑 2012-05-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

指定用户名的时候,应该是用"计算机或域名\用户名"
[/Quote]

那我发布程序后,不再这个域中运行,怎么办呢
新升级版本依然贯彻asp程序员迅速掌握asp.Net的思路,但功能完善,完整的留言板功能都具备了,可以对照前一版本,相信可以更快的学通asp.Net。 程序名称: 深度学习(asp.Net)留言板 v0.1.0 软件类别: asp.Net源码 / 留言 软件语言: 简体中文 授权方式: 免费版 系统平台: asp.Net2.0+ACCESS 程序下载: http://www.DeepTeach.com/ 程序演示: http://www.deepteach.com/ 官方网址: http://www.ITstudy.cn/ 程序开发: 吕海鹏 与前一版本差异: 功能相似度:60%   界面相似度:90%   文件结构相似度:80%   代码结构相似度:80%   代码相似度:30% ---------------------------------------------------------------- 【程序主要功能】 1、精简的增删改查,全部源码开放,适合asp程序员快速理解asp.Net 2、完善的留言、回复、审核、后台管理和分页功能 3、后台留言审核和ip显示 ---------------------------------------------------------------- 【系统开发环境】 1. 系统环境:MS WINDOWS XP +IIS 5.1 2. 数 据 库:MS Access2003 3. 开发工具:MS Visual Studio 2008(C#) 4. 测试浏览器: Firefox3.0.1 微软IE浏览器7.0 5. 系统分辨率:1680X1050 ---------------------------------------------------------------- 【安装使用】 1、将整个目录设置虚拟目录,即可直接访问。 2、请确保你存放的文件everyone可写权限 3、数据库文件名保存在 web.config中,可自行修改设置 ---------------------------------------------------------------- 【版本更新说明】 ver0.1.0 ·增加分页功能 ·增加样式美工 ·增加留言审核 ·增加登陆验证码 ·增加后台管理 ·增加ip记录功能 ·修订了留言排序 ·增加回复功能

110,529

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧