1.首先創建一個類Files
using System.IO;
using System.Security.AccessControl;
namespace 命名空間
{
public class Files
{
/// <summary>
/// 給指定的操作系統用戶賦操作權限
/// </summary>
/// <param name="pathname">文件的路徑</param>
/// <param name="username">操作系統用戶名</param>
/// <param name="power">操作權限,枚舉型:FullControl,ReadOnly,Write,Modify</param>
/// <returns>是否成功</returns>
public static bool addpathPower(string pathname, string username, string power)
{
bool istrue = true;
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;
}
try
{
dirinfo.SetAccessControl(dirsecurity);
}
catch
{
istrue = false;
}
return istrue;
}
}
}
2.之後創建Log類
using System.Text;
using System.IO;
namespace命名空間
{
public class Log
{
#region 記錄錯誤日志
/// <summary>
/// 記錄錯誤日志
/// </summary>
/// <param name="strformat">錯誤信息</param>
/// <param name="path">日志存放位置</param>
public static void WriteErrorLog(string strformat, string path)
{
try
{
if (!File.Exists(path))
{
string nofile = Path.GetDirectoryName(path);
if (!Directory.Exists(nofile))
{
Directory.CreateDirectory(nofile);
Files.addpathPower(nofile, "ASPNET", "FullControl");
}
FileStream fss = File.Create(path); ;
fss.Flush();
fss.Close();//創建之後進行關閉
}
FileStream fs = new FileStream(path, FileMode.Append);
StreamWriter streamWriter = new StreamWriter(fs);
streamWriter.Write(DateTime.Now.ToString() + ":" + strformat + "\r\n" + "--------------------------\r\n");
streamWriter.Flush();
streamWriter.Close();
fs.Close();
}
catch
{
}
}
/// <summary>
/// 記錄錯誤日志
/// </summary>
/// <param name="strformat">錯誤信息,和發送異常位置,系統名</param>
/// <param name="fuzeren">負責人</param>
public static void WriteError(string strformat, string fuzeren)
{
try
{
WriteErrorLog("負責人:" + fuzeren + strformat, "c://Program Files//cutt//error.txt");
string path = "c://Program Files//cutt//" + fuzeren + "_error.txt";
if (!File.Exists(path))
{
string nofile = Path.GetDirectoryName(path);
if (!Directory.Exists(nofile))
{
Directory.CreateDirectory(nofile);
Files.addpathPower(nofile, "ASPNET", "FullControl"); //給aspnet用戶加權限,避免權限不夠不能寫入服務器文件
}
FileStream fss =File.Create(path);;
fss.Flush();
fss.Close();//創建之後進行關閉 ,否則直接寫入報錯
}
FileStream fs = new FileStream(path, FileMode.Append);
StreamWriter streamWriter = new StreamWriter(fs);
streamWriter.Write(DateTime.Now.ToString() + ":" + strformat + "\r\n" + "--------------------------\r\n");
streamWriter.Flush();
streamWriter.Close();
fs.Close();
}
catch
{
}
}
#endregion
}
}
3.之後在catch中調用Log類的方法就OK了~
注: Files類和Log類要在一個命名空間下。當然也可以不在,但需要修改以上代碼的一些細節部分。