程序没有收集目录信息
本文关键字:信息 程序 | 更新日期: 2023-09-27 17:49:25
我正在编写一个程序,用于收集机器信息以排除故障。在对以管理员身份运行的程序进行了一些工作后,我无法使它正确输出%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运行,只是更新了Windows 7和VS.我检查了任何奇怪的文件过滤器管理器驱动程序,但我没有看到任何。
目录和文件信息缺失
我简化了我的问题,发现我没有考虑到环境中的可视化。