我正在制作一个程序,收集有关机器的信息以进行故障排除。在以管理员身份运行程序的一些工作之后,我无法正确输出%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.我检查了任何奇怪的文件过滤器管理器驱动程序,但我没有看到任何。