我不知道任何“已批准”API从Windows应用商店应用中获取此信息。
但是,如果您只想在测试应用程序中执行此操作,则可以使用“未经批准”的API。如果您使用未经批准的API,您的程序将无法通过WACK认证,并且无法上传到商店,但在本地进行测试即可。请注意,调用未经批准的API时没有保证,因此行为在形式上是未定义的,但许多函数都可以正常工作 - 至少在当前版本中。
我演示了如何在我今年夏天写的文章"printf debugging in Metro style apps"中的文章中调用AllocConsole
。你可以用C#做类似的事情,或者使用P/Invoke来调用本地函数,或者编写一个可以从C#调用的C++ Windows运行时组件。
对于您的情况,我建议调用GetCurrentProcess
来获取当前进程的句柄并将其传递到GetProcessTimes
以获取用户和内核时间。我使用这种方法进行了简短的测试并获得了合理的结果。
下面是一个使用P/Invoke来拨打电话一个完整的C#类:
static class DebugProcessTimes
{
[StructLayout(LayoutKind.Sequential)]
private struct FileTime
{
public UInt32 Low;
public UInt32 High;
}
private static UInt64 ToUInt64(FileTime time)
{
return ((UInt64)time.High << 32) + time.Low;
}
[DllImport("kernel32.dll")]
private static extern IntPtr GetCurrentProcess();
[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetProcessTimes(
IntPtr hProcess,
out FileTime lpCreationTime,
out FileTime lpExitTime,
out FileTime lpKernelTime,
out FileTime lpUserTime);
public struct ProcessTimes
{
public UInt64 CreationTime;
public UInt64 ExitTime;
public UInt64 KernelTime;
public UInt64 UserTime;
}
public static ProcessTimes GetProcessTimes()
{
FileTime creation, exit, kernel, user;
if (!GetProcessTimes(GetCurrentProcess(),
out creation, out exit, out kernel, out user))
throw new Exception(":'(");
return new ProcessTimes
{
CreationTime = ToUInt64(creation),
ExitTime = ToUInt64(exit),
KernelTime = ToUInt64(kernel),
UserTime = ToUInt64(user)
};
}
}
用法:
var times = DebugProcessTimes.GetProcessTimes();
有趣!由于pinvoke.net上的声明依赖于类似于System.Runtime.InteropServices.FILETIME的类型,这些类型在WinRT中没有公开,所以你介意发布你的P/Invoke代码,因为它听起来像你已经困惑了? –
我没有写过P/Invoke代码,但是我迅速地试了一下。查看更新。结果看起来很合理。 –