程序不收集目录信息

Program not gathering directory information

我正在开发一个程序,该程序收集有关机器的信息以进行故障排除。在以管理员身份对程序 运行 进行了一些工作后,我无法让它正确输出 %windir%\drivers 中的所有文件。我还 运行 解决了当前用户 appdata 的问题,它抛出 UnauthorizedAccessException 并且不输出任何内容。我还好,我会写一个服务来解决这个问题。在获得基本服务进行测试后。我仍然没有看到输出。这是服务的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace SVCTest
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            while (true)
            {
                OutputDirStructureToFile(
                    Environment.GetEnvironmentVariable("userprofile") + "\Desktop\" + Path.GetRandomFileName() + ".txt", 
                    Environment.ExpandEnvironmentVariables("windir") + "\drivers");
                System.Threading.Thread.Sleep(30000);
            }
        }

        protected override void OnStop()
        {
        }

        /// <summary>
        /// Outputs the structure of a directory to a file.
        /// Uses GetFileStructureRecursive.
        /// </summary>
        /// <param name="outputFileName">The file to be outputed to.</param>
        /// <param name="folder">Directory to get the structure from.</param>
        /// <param name="searchPatern">What to search for. EXAMPLE: *.*</param>
        private void OutputDirStructureToFile(string outputFileName, string folder)
        {
            using (var file = new StreamWriter(outputFileName))
            {
                file.Write(GetFileStrucutre(new DirectoryInfo(folder), "*.*"));
            }
        }
        private string GetFileStrucutre(DirectoryInfo dirInfo, string searchPattern)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine();
            sb.AppendLine("Root " + dirInfo.Root);
            sb.AppendLine();

            sb.Append(GetFileStructureRecursive(dirInfo, searchPattern));

            return sb.ToString();

        }
        private string GetFileStructureRecursive(DirectoryInfo dirInfo, string searchPattern)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("\r\n Directory of " + dirInfo.FullName + "\r\n");

            foreach (var x in dirInfo.GetFileSystemInfos(searchPattern))
            {
                sb.AppendLine(
                    x.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture).PadRight(25)
                    + x.Name.PadRight(50)
                    );
            }

            foreach (var dir in dirInfo.GetDirectories())
            {
                sb.Append(GetFileStructureRecursive(dir, searchPattern));
            }

            //try
            //{
            //    foreach (var dir in dirInfo.GetDirectories(searchPattern))
            //    {

            //        sb.AppendLine(
            //            dir.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture).PadRight(25)
            //            + String.Empty.PadRight(15)
            //            + dir.Name.PadRight(50)
            //            + dir.Attributes.ToString().PadRight(50)
            //            + dir.LastAccessTime.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture).PadRight(25)
            //            + dir.CreationTime.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)
            //            );
            //    }
            //}
            //catch (UnauthorizedAccessException) { }

            //try
            //{
            //    foreach (var file in dirInfo.GetFiles(searchPattern))
            //    {

            //        sb.AppendLine(
            //            file.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture).PadRight(25)
            //            + file.Length.ToString("N0").PadRight(15)
            //            + file.Name.PadRight(50)
            //            + file.Attributes.ToString().PadRight(50)
            //            + file.LastAccessTime.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture).PadRight(25)
            //            + file.CreationTime.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)
            //            );
            //    }
            //}
            //catch (UnauthorizedAccessException) { }

            //try
            //{
            //    foreach (var dir in dirInfo.GetDirectories())
            //    {
            //        sb.Append(GetFileStructureRecursive(dir, searchPattern));
            //    }
            //}
            //catch (UnauthorizedAccessException) { }

            return sb.ToString();
        }

    }
}

您会注意到我注释掉了一大块,认为我可能一次访问了太多信息。这是我在安装并启动服务后得到的结果:

Root C:\

 Directory of C:\Windows\System32\drivers

2011-04-12 01:38:56      en-US                                             
2009-06-10 15:14:29      gm.dls                                            
2009-06-10 15:14:29      gmreadme.txt                                      
2011-04-12 01:38:56      UMDF                                              
2009-07-13 19:19:10      wimmount.sys                                      

 Directory of C:\Windows\System32\drivers\en-US

2011-04-12 01:38:25      bfe.dll.mui                                       
2011-04-12 01:38:22      ndiscap.sys.mui                                   
2011-04-12 01:38:25      pacer.sys.mui                                     
2011-04-12 01:38:27      qwavedrv.sys.mui                                  
2011-04-12 01:38:22      scfilter.sys.mui                                  
2011-04-12 01:38:20      tcpip.sys.mui                                     

 Directory of C:\Windows\System32\drivers\UMDF

2011-04-12 01:38:56      en-US                                             

 Directory of C:\Windows\System32\drivers\UMDF\en-US

其中肯定缺少一些关键驱动程序。对于我的生活,我无法弄清楚为什么它不收集信息。没有AV运行,只是更新了Windows7和VS。我检查了是否有任何奇怪的文件过滤器管理器驱动程序,但我没有看到任何驱动程序。

我简化了我的问题,发现我没有考虑环境中的可视化。