2

I am working on a program that gathers information about the machine for troubleshooting purposes. After some work on the program running as admin, I couldn't get it to output all the files in the %windir%\drivers properly. I also ran into issues with the current users appdata where it throws a UnauthorizedAccessException and doesn't output anyhing. I though okay, I'll write a service to fix this. After getting a basic service down for testing. I'm still not seeing output. Here is the code for the service:

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();
        }

    }
}

You will notice that I commented out a chunk thinking I may be accessing too much info at once. Here is what I am getting after installing and starting the service:

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

Definitely missing some key drivers in there. For the life of me I can't figure out why it isn't gathering the information. There is no AV running, just updated Windows 7 and VS. I checked for any odd file filter manager drivers but I'm not seeing any.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Johnny Shaw
  • 157
  • 1
  • 6
  • Small correction here, when I was moving the code over and re-writing the `OutputDirStructureToFile` call into two lines. I only put %windir%\drivers. not %windir%\System32\drivers like the output shows. Also when I mention up top %windir%\drivers should be %windir%\System32\drivers. Sorry about this mix-up. But you can see the output starts with %windir%\System32\drivers which shows I ran it with the proper inputs. – Johnny Shaw May 18 '15 at 13:46
  • I have found that if I get the file and directory paths before gathering the list of file information I get more output. For example: `foreach (var x in Directory.GetDirectories(dirInfo.FullName, searchPattern, SearchOption.AllDirectories) { DirectoryInfo d = new DirectoryInfo(x);` and similarly doing the same thing for files I get more output but it is still missing some. – Johnny Shaw May 18 '15 at 15:54
  • Okay lets simplify this. The following code does not return the full directory info. `IEnumerable files = new DirectoryInfo("C:\\Windows\\System32\\drivers").EnumerateFileSystemInfos("*", SearchOption.AllDirectories);` – Johnny Shaw May 18 '15 at 16:55

1 Answers1

0

Missing directory and file info

I simplified my questions and discovered that I wasn't considering the visualization within the environment.

Community
  • 1
  • 1
Johnny Shaw
  • 157
  • 1
  • 6
  • I also wanted to note that C:\Windows\Sysnative will return the native System32 of the machine regardless of the architecture. – Johnny Shaw May 22 '15 at 21:08