我用 C# 语言 (.net) 编写了一个小型桌面应用程序(主窗体)。而且我想让我的应用程序位于开始菜单(Windows 8)的顶部,就像“camtasia studio screenrecoder”一样。
查看我想要的小应用程序的屏幕截图。
我必须在我的应用程序中添加什么代码?
注意:我尝试设置topMost = true
, 但这不起作用。
If you want a window on top of Metro, you need it to declare accessibility. Here are the key points:
The application must demand uiAccess (app.manifest)
The application must assert “topmost” window positioning (either in Win32/SetWindowPos or WinForms/WPF’s “Topmost” property, programmatically or otherwise)
Without making changes to the group policy setting, it must be installed to some trusted location [C:\Windows, C:\Program Files, C:\Program Files (x86)].
a. Note: If you want to be able to run it out of an arbitrary location, you must disable the security setting: “User Account Control: Only elevate UIAccess applications that are installed in secure locations”.
b. Note2: This is the same as setting HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System\ValidateAdminCodeSignatures to 0
Said application cannot be ran in the debugger
If it’s a .NET application
a. The manifest must be embedded in a post-build step
b. The application must have “delayed signing” (meaning it cannot be ran from the built-in debugger, although you can build and attach – this is what Microsoft does)
The application must be signed with a trusted certificate.
Said trusted certificate must be installed to the Trusted Root Certificate Authority (this is important! It must not just simply installed)
For more info see: http://msdn.microsoft.com/en-us/library/ms726294
为了强制您的应用程序高于 Metro 的用户界面,您需要执行以下操作:
CreateWindowEX
和set WS_EX_TOPMOST
Project.Properties
并链接到清单文件。/uiAccess = "true"
SignTool
对应用程序进行签名。Program Files
或Program Files (x86)
Start Menu
的应用程序,您的应用程序应该在 Metro 之上运行。您的清单应如下所示:
<trustInfo xmlns="urn:0073chemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel
level="highestAvailable"
UIAccess="true" />
</requestedPrivileges>
</security>
</trustInfo>
默认情况下,false
如果省略该属性,或者您的程序集不存在清单,则将其设置为。有了它,false
您将无法访问ProtectedUI
.
更多关于安全的信息可以在这里找到:
这是一个可以工作或允许修改以测试 UAC 的脚本:
class Elevated_Rights
{
// Token Bool:
private bool _level = false;
#region Constructor:
protected Elevated_Rights()
{
// Invoke Method On Creation:
Elevate();
}
#endregion
public void Elevate()
{
// Get Identity:
WindowsIdentity user = WindowsIdentity.GetCurrent();
// Set Principal
WindowsPrincipal role = new WindowsPrincipal(user);
#region Test Operating System for UAC:
if (Environment.OSVersion.Platform != PlatformID.Win32NT || Environment.OSVersion.Version.Major < 6)
{
// False:
_level = false;
}
#endregion
else
{
#region Test Identity Not Null:
if (user == null)
{
// False:
_level = false;
}
#endregion
else
{
#region Ensure Security Role:
if (!(role.IsInRole(WindowsBuiltInRole.Administrator)))
{
// False:
_level = false;
}
else
{
// True:
_level = true;
}
#endregion
}
}
}
类似的东西可以确保您可以处理或至少提醒用户该功能可能无法正常工作。请注意,在上面我实际上保护了调用并调用了方法;这样我就可以随时访问该_level
值以确保身份验证仍然存在。并且仅在需要时继承或使用它以避免不必要的调用。希望这会有所帮助。
更新评论:
这适用于您的 C# 项目,您可以调用以下代码:
using System.Diagnostics;
上述组件将为您提供该功能。然后在一个方法中调用以下内容。
Process command = new Process();
command.StartInfo.FileName = "notepad.exe";
command.Start();
正如您所看到的,它不是技术性的,但它允许您batch
调用msiexec
. 希望这会有所帮助。