[英]Dictionary Object in c#
我是編碼新手,我已經開始在 c# 中處理一項任務。 我必須開發一個代碼來獲取文件信息,比如文件類型、文件大小、給定目錄路徑輸入的所有者名稱。
現在,為了節省時間,我想建立一個字典,我將在其中存儲所有 SID 和相應的所有者信息。 並且代碼不會每次都通過轉換每個文件的 SID 來循環獲取所有者名稱,而是會獲取該文件的 SID 並使用構建的字典將其映射到它的所有者。 這本字典將被構建一次,如果有新的所有者加入,它將被更新。 有誰知道如何創建一個可以單獨使用的字典。
這是我正在處理的代碼-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Diagnostics;
using System.Collections;
using Alphaleonis.Win32.Filesystem;
// Alpha is external library used to allow long paths.
namespace Get_SID_Owner_Info
{
internal class Program
{
private static void Main()
{
Console.Write("Please enter the Directory Path -- ");
string foldr = Console.ReadLine();
Console.Write("Please enter the result path location e.g. D:\\Nolder\\Outfile.csv -- ");
string outfile = Console.ReadLine();
//string foldr = "D:\\Ansys_Training";
//string outfile = "D:\\Get_SID_Owner.csv";
var watch = new System.Diagnostics.Stopwatch();
watch.Start();
int[] index = new int[1000000];
int i;
i = 0;
IdentityReference[] SID_store = new IdentityReference[1000000];
IdentityReference[] Owner_store = new IdentityReference[1000000];
if (File.Exists(outfile))
{
File.Delete(outfile);
}
// Create a new file
using (System.IO.StreamWriter sw = File.CreateText(outfile))
{
sw.WriteLine("{0},{1}", "SID", "Owner Name");
string[] files = System.IO.Directory.GetFiles(foldr);
DirectoryInfo tempWithoutMac = new DirectoryInfo(foldr);
foreach (FileInfo fi in tempWithoutMac.GetFiles())
{
// SID --
FileSecurity fs = File.GetAccessControl(fi.FullName);
IdentityReference SID = fs.GetOwner(typeof(SecurityIdentifier));
// Owner --
IdentityReference Owner = SID.Translate(typeof(NTAccount));
SID_store[i] = SID;
Owner_store[i] = Owner;
i = i + 1;
}
foreach (string d in System.IO.Directory.GetDirectories(foldr, "*", System.IO.SearchOption.AllDirectories))
{
tempWithoutMac = new DirectoryInfo(d);
foreach (FileInfo fi in tempWithoutMac.GetFiles())
{
// SID --
FileSecurity fs = File.GetAccessControl(fi.FullName);
IdentityReference SID = fs.GetOwner(typeof(SecurityIdentifier));
// Owner --
IdentityReference Owner = SID.Translate(typeof(NTAccount));
SID_store[i] = SID;
Owner_store[i] = Owner;
i = i + 1;
}
}
IdentityReference[] SID_store2 = new IdentityReference[i];
IdentityReference[] Owner_store2 = new IdentityReference[i];
for (int j = 0; j < i; j++)
{
SID_store2[j] = SID_store[j];
Owner_store2[j] = Owner_store[j];
}
var SID_Unique = SID_store2.Distinct().ToList(); // Contains Unique SID's for the given directory --
var Owner_Unique = Owner_store2.Distinct().ToList();
Dictionary<IdentityReference, IdentityReference> SID_Owner_Data = new Dictionary<IdentityReference, IdentityReference>();
for (int j = 0; j < SID_Unique.Count; j++) // SID to Owner conversion for the Unique SID's --
{
SID_Owner_Data.Add(SID_Unique[j], Owner_Unique[j]);
Console.WriteLine(SID_Unique[j]);
Console.WriteLine(Owner_Unique[j]);
}
Console.WriteLine(SID_Unique.Count);
for (int k = 0; k < SID_Unique.Count; k++)
{
sw.WriteLine("{0},{1}", SID_Unique[k], Owner_Unique[k]);
}
}
watch.Stop();
Console.WriteLine($"Execution Time: {watch.ElapsedMilliseconds} ms");
Console.ReadKey();
}
}
}
如果我理解正確,您希望能夠將 SID 解析為其所有者,但通過緩存機制使每個 SID 僅解析一次。 這與 ConcurrentDictionary 和GetOrAdd
無關。
ConcurrentDictionary<IdentityReference,IdentityReference> _cache = new ConcurrentDictionary<IdentityReference,IdentityReference>();
IdentityReference GetTranslationWithCache(IdentityReference SID)
{
return _cache.GetOrAdd( SID, () => SID.Translate(typeof(NTAccount));
}
在這個例子中。 GetOrAdd
將在緩存中搜索 SID,如果找到則返回相應的翻譯。 如果未找到,則將該鍵添加到字典中並調用委托 ( () => SID.Translate
) 以填充其值。 這使它非常適合用作緩存。 作為獎勵,您的字典是線程安全的,因此您可以從多個線程填充它以提高性能,並且仍然保證每個 SID 對Translate
的調用只會發生一次。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.