Mini-Forensik: durchsucht ein Verzeichnis rekursiv nach Dateien,
die ein Suchwort enthalten (z. B. "Exception").
C#
// super simple, kein async, Demo
void Search(string root, string pattern)
{
foreach (var file in Directory.EnumerateFiles(
root, "*.*",
SearchOption.AllDirectories))
{
var text = File.ReadAllText(file);
if (text.Contains(pattern,
StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine(file);
}
}
}
// Beispiel:
Search(@"C:\inetlogs", "Exception");
📤
AD-User-Export → Excel
Holt Benutzerinfos aus dem Active Directory
(DisplayName, SamAccountName, Abteilung, Mail)
und schreibt direkt eine .xlsx-Datei.
Praktisch für Audit / Lizenz / Onboarding-Übersicht.
Liest AD-User über DirectoryServices aus
und bereitet die Daten für Excel auf.
Die Excel-Erstellung unten nutzt z. B. ClosedXML.
C#
// NuGet:
// System.DirectoryServices.AccountManagement
// ClosedXML (oder EPPlus)
// using System.DirectoryServices.AccountManagement;
// using ClosedXML.Excel;
public class AdUserInfo {
public string DisplayName { get; set; }
public string SamAccountName { get; set; }
public string Department { get; set; }
public string Mail { get; set; }
}
public static List GetAdUsers() {
var list = new List();
using (var ctx = new PrincipalContext(
ContextType.Domain, "FIRMA.LOCAL"))
{
using (var q = new UserPrincipal(ctx))
using (var searcher = new PrincipalSearcher(q))
{
foreach (var r in searcher.FindAll()) {
if (r is UserPrincipal u) {
list.Add(new AdUserInfo {
DisplayName = u.DisplayName,
SamAccountName = u.SamAccountName,
Department = u.GetUnderlyingObject()
is DirectoryEntry de
? (string)de.Properties["department"].Value
: null,
Mail = u.EmailAddress
});
}
}
}
}
return list;
}
public static void ExportToExcel(List users,
string path) {
using (var wb = new XLWorkbook()) {
var ws = wb.Worksheets.Add("Users");
ws.Cell(1,1).Value = "DisplayName";
ws.Cell(1,2).Value = "SamAccountName";
ws.Cell(1,3).Value = "Department";
ws.Cell(1,4).Value = "Mail";
int row = 2;
foreach (var u in users) {
ws.Cell(row,1).Value = u.DisplayName;
ws.Cell(row,2).Value = u.SamAccountName;
ws.Cell(row,3).Value = u.Department;
ws.Cell(row,4).Value = u.Mail;
row++;
}
ws.Columns().AdjustToContents();
wb.SaveAs(path);
}
}
// Usage:
var data = GetAdUsers();
ExportToExcel(data, @"C:\temp\ad_users.xlsx");