I want to write a fast disk usage detect program, and found that FileSystemObject
is the fastest way to do this. And FileSystemObject
is in COM -> Microsoft Scripting Runtime
.
All the code is simple, and I parsed here.
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();
}
}
}
And I setup the app.manifest
to
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
When I test my program, Security exception come at this line
Int64 dirSize = (Int64)folder.Size;
The Exception Info( I translate manually, sorry for my poor English. ) is
Unhandled exception type “System.Security.SecurityException” occurred in DiskUsage.exe
Other message: exception from HRESULT:0x800A0046 (CTL_E_PERMISSIONDENIED)
If I change FolderPath
to @"D:\Codes"
. It works fine. So I think the security setting in manifest is not effect to COM -> Microsoft Scripting Runtime
. Anyone know how to fix this? Please help, thanks.