![](/img/trans.png)
[英]PermissiveModifyControl throws DirectoryOperationException in C# LDAP
[英]getting info from DirectoryOperationException in C#
我们有一个C#项目,该项目与Active Directory服务进行交互。
对于上下文:我们使用System.DirectoryServices.Protocols命名空间中的对象,即:
我们卡住了有一段时间了理解由此引发了DirectoryOperationException,实现什么不包括在错误的描述之前的错误exception.Message
,但被嵌套在异常对象进一步下跌。
捕获此类错误时,我们曾经有一个非常简单的异常日志记录:
catch (DirectoryOperationError de) {
log("ERROR directory error {0} : {1}", de.GetType(), de.Message);
throw;
}
我们现在有以下代码:
catch (DirectoryOperationException de)
{
log("ERROR directory error {0} : {1}", de.GetType(), de.Message);
var resp = de.Response;
if (resp == null)
{
log(" -- no response object linked to exception --");
throw;
}
log("ERROR directoryresponse error message:'{0}'", resp.ErrorMessage);
int errorCode;
var hexCode = resp.ErrorMessage.Substring(0, 8);
if (!int.TryParse(hexCode, System.Globalization.NumberStyles.HexNumber, null, out errorCode)){
log(" -- could not figure out error code from '{0}' --", hexCode);
throw;
}
var win32exception = new System.ComponentModel.Win32Exception(errorCode);
var msg = win32exception.Message;
log("ERROR errcode:{0} : {1}", errorCode, msg);
throw;
}
在我的“ hocus pocus”量表上排名很高(尤其是我们依赖于以8个字符长的十六进制整数开头的字符串消息的部分)。
题
有没有更直接的方法来访问基础LDAPError并使用C#将其转换为有意义的消息?
有使用目录服务的理由吗? 您可以共享文件夹并使用以下代码吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace SAveDirectoriesXml
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
const string FOLDER = @"c:\temp";
static XmlWriter writer = null;
static void Main(string[] args)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
writer = XmlWriter.Create(FILENAME, settings);
writer.WriteStartDocument(true);
DirectoryInfo info = new DirectoryInfo(FOLDER);
WriteTree(info);
writer.WriteEndDocument();
writer.Flush();
writer.Close();
Console.WriteLine("Enter Return");
Console.ReadLine();
}
static long WriteTree(DirectoryInfo info)
{
long size = 0;
writer.WriteStartElement("Folder");
try
{
writer.WriteAttributeString("name", info.Name);
writer.WriteAttributeString("numberSubFolders", info.GetDirectories().Count().ToString());
writer.WriteAttributeString("numberFiles", info.GetFiles().Count().ToString());
writer.WriteAttributeString("date", info.LastWriteTime.ToString());
foreach (DirectoryInfo childInfo in info.GetDirectories())
{
size += WriteTree(childInfo);
}
}
catch (Exception ex)
{
string errorMsg = string.Format("Exception Folder : {0}, Error : {1}", info.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error", errorMsg);
}
FileInfo[] fileInfo = null;
try
{
fileInfo = info.GetFiles();
}
catch (Exception ex)
{
string errorMsg = string.Format("Exception FileInfo : {0}, Error : {1}", info.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error",errorMsg);
}
if (fileInfo != null)
{
foreach (FileInfo finfo in fileInfo)
{
try
{
writer.WriteStartElement("File");
writer.WriteAttributeString("name", finfo.Name);
writer.WriteAttributeString("size", finfo.Length.ToString());
writer.WriteAttributeString("date", info.LastWriteTime.ToString());
writer.WriteEndElement();
size += finfo.Length;
}
catch (Exception ex)
{
string errorMsg = string.Format("Exception File : {0}, Error : {1}", finfo.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error", errorMsg);
}
}
}
writer.WriteElementString("size", size.ToString());
writer.WriteEndElement();
return size;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.