【发布时间】:2016-04-16 08:10:44
【问题描述】:
我想写一个快速的磁盘使用检测程序,发现FileSystemObject是最快的方法。而FileSystemObject 在COM -> Microsoft Scripting Runtime 中。
所有代码都很简单,我这里解析一下。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DiskUsage
{
class Program
{
const string FolderPath = @"C:\Windows\System32";
static void Main(string[] args)
{
var startTime = DateTime.Now;
Scripting.FileSystemObject fso = new Scripting.FileSystemObject();
Scripting.Folder folder = fso.GetFolder(FolderPath);
Int64 dirSize = (Int64)folder.Size;
TimeSpan span = DateTime.Now.Subtract(startTime);
System.Console.WriteLine("{1} size: {0}", dirSize, FolderPath);
System.Console.WriteLine("use time: {0}", span);
System.Console.ReadKey();
}
}
}
我将app.manifest 设置为
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
当我测试我的程序时,安全异常出现在这一行
Int64 dirSize = (Int64)folder.Size;
异常信息(我手动翻译,对不起我的英语不好。)是
Unhandled exception type “System.Security.SecurityException” occurred in DiskUsage.exe
Other message: exception from HRESULT:0x800A0046 (CTL_E_PERMISSIONDENIED)
如果我将FolderPath 更改为@"D:\Codes"。它工作正常。所以我认为清单中的安全设置对COM -> Microsoft Scripting Runtime 没有影响。有人知道怎么修这个东西吗?请帮忙,谢谢。
【问题讨论】:
标签: c# security com uac filesystemobject