您的位置:首页 > 编程语言 > C#

C#应用程序的UAC控制,提升应用程序权限。获得版本号码

2012-08-22 13:36 417 查看
#region
/// <summary>
/// 设置文件的访问权限
/// </summary>
/// <param name="filePath"> 路径</param>
/// <param name="username"> 用户名</param>
/// SetAccount(@"C:\eee.txt", "BATCH");
public static void SetAccount(string filePath, string username)
{

FileInfo fileInfo = new FileInfo(filePath);

FileSecurity fileSecurity = fileInfo.GetAccessControl();

fileSecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.FullControl, AccessControlType.Allow));     //以完全控制为例

fileInfo.SetAccessControl(fileSecurity);

}
/// <summary>
/// 设置路径的访问权限
/// </summary>
/// <param name="FolderPath">路径</param>
/// <param name="UserName">用户名</param>
/// <param name="Rights">权限</param>
/// <param name="AllowOrDeny">可访问</param>
/// <returns></returns>
/// 调用方式SetFolderACL("C:\\test", "BATCH", FileSystemRights.FullControl, AccessControlType.Allow);
public static bool SetFolderACL(String FolderPath, String UserName, FileSystemRights Rights, AccessControlType AllowOrDeny)
{

InheritanceFlags inherits = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;

return SetFolderACL(FolderPath, UserName, Rights, AllowOrDeny, inherits, PropagationFlags.None, AccessControlModification.Add);

}

public static bool SetFolderACL(String FolderPath, String UserName, FileSystemRights Rights, AccessControlType AllowOrDeny, InheritanceFlags Inherits, PropagationFlags PropagateToChildren, AccessControlModification AddResetOrRemove)
{

bool ret;

DirectoryInfo folder = new DirectoryInfo(FolderPath);

DirectorySecurity dSecurity = folder.GetAccessControl(AccessControlSections.All);

FileSystemAccessRule accRule = new FileSystemAccessRule(UserName, Rights, Inherits, PropagateToChildren, AllowOrDeny);
dSecurity.ModifyAccessRule(AddResetOrRemove, accRule, out ret);
folder.SetAccessControl(dSecurity);

return ret;

}
#endregion
using System.Deployment.Application;Content.Text = "程序集版本:" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() +"\n";Content.Text += "文件版本:" + Application.ProductVersion.ToString() +"\n";Content.Text += "部署版本:" + ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();//其实部署版本没有多大用处。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: