Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c语言中的Dictionary对象#_C#_Dictionary - Fatal编程技术网

C# c语言中的Dictionary对象#

C# c语言中的Dictionary对象#,c#,dictionary,C#,Dictionary,我对编码还不熟悉,我已经开始用c#来完成一项任务。我必须开发一个代码来获取文件信息,比如给定目录路径输入的文件类型、文件大小、所有者名称 现在,为了节省时间,我考虑构建一个字典,在其中存储所有SID和相应的所有者信息。代码不会通过每次转换每个文件的SID来循环获取所有者名称,相反,它将获取该文件的SID,并使用构建的字典将其映射到所有者。此词典将生成一次,如果有新所有者加入,则将更新。 有人知道如何创建一个可以单独使用的词典吗 这是我正在编写的代码-- 使用系统; 使用System.Collec

我对编码还不熟悉,我已经开始用c#来完成一项任务。我必须开发一个代码来获取文件信息,比如给定目录路径输入的文件类型、文件大小、所有者名称

现在,为了节省时间,我考虑构建一个字典,在其中存储所有SID和相应的所有者信息。代码不会通过每次转换每个文件的SID来循环获取所有者名称,相反,它将获取该文件的SID,并使用构建的字典将其映射到所有者。此词典将生成一次,如果有新所有者加入,则将更新。 有人知道如何创建一个可以单独使用的词典吗

这是我正在编写的代码--

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
//使用System.IO;
使用System.Security.AccessControl;
使用System.Security.Principal;
使用系统诊断;
使用系统集合;
使用Alphaleonis.Win32.Filesystem;
//Alpha是用于允许长路径的外部库。
命名空间获取\u SID\u所有者\u信息
{
内部课程计划
{
私有静态void Main()
{
编写(“请输入目录路径--”);
字符串foldr=Console.ReadLine();
Console.Write(“请输入结果路径位置,例如D:\\Nolder\\Outfile.csv-->”);
string outfile=Console.ReadLine();
//字符串foldr=“D:\\Ansys\u培训”;
//string outfile=“D:\\Get\u SID\u Owner.csv”;
var-watch=新系统.Diagnostics.Stopwatch();
watch.Start();
int[]索引=新int[1000000];
int i;
i=0;
IdentityReference[]SID_store=新IdentityReference[1000000];
IdentityReference[]所有者\门店=新IdentityReference[1000000];
if(File.Exists(outfile))
{
文件。删除(输出文件);
}
//创建一个新文件
使用(System.IO.StreamWriter sw=File.CreateText(outfile))
{
sw.WriteLine(“{0},{1}”,“SID”,“所有者名称”);
string[]files=System.IO.Directory.GetFiles(foldr);
DirectoryInfo tempWithoutMac=新的DirectoryInfo(foldr);
foreach(tempWithoutMac.GetFiles()中的FileInfo fi)
{
//希德--
FileSecurity fs=File.GetAccessControl(fi.FullName);
IdentityReference SID=fs.GetOwner(typeof(SecurityIdentifier));
//所有者--
IdentityReference Owner=SID.Translate(typeof(NTAccount));
SID_store[i]=SID;
所有者\门店[i]=所有者;
i=i+1;
}
foreach(System.IO.Directory.GetDirectories(foldr,“*”,System.IO.SearchOption.AllDirectories)中的字符串d)
{
tempWithoutMac=新目录信息(d);
foreach(tempWithoutMac.GetFiles()中的FileInfo fi)
{
//希德--
FileSecurity fs=File.GetAccessControl(fi.FullName);
IdentityReference SID=fs.GetOwner(typeof(SecurityIdentifier));
//所有者--
IdentityReference Owner=SID.Translate(typeof(NTAccount));
SID_store[i]=SID;
所有者\门店[i]=所有者;
i=i+1;
}
}
IdentityReference[]SID_store2=新的IdentityReference[i];
IdentityReference[]所有者\仓库2=新IdentityReference[i];
对于(int j=0;j
如果我理解正确,您希望能够通过缓存机制将SID解析给其所有者,以便每个SID只解析一次。使用ConcurrentDictionary和

ConcurrentDictionary\u cache=new ConcurrentDictionary();
IdentityReference GetTranslationWithCache(IdentityReference SID)
{
返回_cache.GetOrAdd(SID,()=>SID.Translate(typeof(NTAccount));
}

在本例中,
GetOrAdd
将在缓存中搜索SID,如果找到,则返回相应的翻译。如果找不到,则将密钥添加到字典和委托(
()=>SID.Translate
)调用以填充其值。这使其很好地用作缓存。另外,您的字典是线程安全的,因此您可以从多个线程填充它以提高性能,并且仍然可以保证对
Translate
的调用每个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();
        }
    }
}
ConcurrentDictionary<IdentityReference,IdentityReference> _cache = new ConcurrentDictionary<IdentityReference,IdentityReference>();

IdentityReference GetTranslationWithCache(IdentityReference SID)
{
    return _cache.GetOrAdd( SID, () => SID.Translate(typeof(NTAccount));
}