本篇文章主要介绍了" 在Winform程序中设置管理员权限及为用户组添加写入权限",主要涉及到方面的内容,对于.NETjrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下:
在我们一些Winform程序中,往往需要具有一些特殊的权限才能操作系统文件,我们可以设置运行程序具有管理员权限或者设置运行程序的目录具有写入的权限,如果是在操作...
在程序运行的时候,会提示“用户账户控制”来获取管理员权限运行,点击“是”则获取了管理员权限。
对于需要为指定目录设置用户组权限,那么也是可以通过C#代码进行处理的。
一般情况下,我们可以在程序安装或者启动的时候,对目录进行用户组权限的处理,这样程序运行起来就自然具有对应目录的读写权限了。
如我们在程序启动的时候处理,那么我们可以在Main函数的里面进行设置。
////// 应用程序的主入口点。
/// [STAThread]
privatestaticvoid Main()
{
}
为了方便处理,我们添加一个公共的函数,用来处理用户组的目录权限访问操作,C#代码如下所示。
////// 为指定用户组,授权目录指定完全访问权限
//////用户组,如Users///实际的目录///privatestaticbool SetAccess(string user, string folder)
{
//定义为完全控制的权限const FileSystemRights Rights = FileSystemRights.FullControl;
//添加访问规则到实际目录var AccessRule = new FileSystemAccessRule(user, Rights,
InheritanceFlags.None,
PropagationFlags.NoPropagateInherit,
AccessControlType.Allow);
var Info = new DirectoryInfo(folder);
var Security = Info.GetAccessControl(AccessControlSections.Access);
bool Result;
Security.ModifyAccessRule(AccessControlModification.Set, AccessRule, out Result);
if (!Result) returnfalse;
//总是允许再目录上进行对象继承const InheritanceFlags iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
//为继承关系添加访问规则 AccessRule = new FileSystemAccessRule(user, Rights,
iFlags,
PropagationFlags.InheritOnly,
AccessControlType.Allow);
Security.ModifyAccessRule(AccessControlModification.Add, AccessRule, out Result);
if (!Result) returnfalse;
Info.SetAccessControl(Security);
returntrue;
}
然后我们在Main函数里面进行调用就可以了。
////// 应用程序的主入口点。
/// [STAThread]
privatestaticvoid Main()
{
//为用户组指定对应目录的完全访问权限 SetAccess("Users", Application.StartupPath);
//界面汉化 System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-Hans");
DevExpress.UserSkins.BonusSkins.Register();
DevExpress.Skins.SkinManager.EnableFormSkins();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
//登录界面 Login dlg = new Login();
dlg.StartPosition = FormStartPosition.CenterScreen;
if (DialogResult.OK == dlg.ShowDialog())
{
if (dlg.bLogin)
{
SplashScreen.Splasher.Show(typeof(SplashScreen.frmSplash));
gc.MainDialog = new MainForm();
gc.MainDialog.StartPosition = FormStartPosition.CenterScreen;
Application.Run(gc.MainDialog);
}
}
dlg.Dispose();
}
这样在程序运行后,我们就可以看到对应目录具有完全的读写操作权限了,这样对于一些如读写SQLite出错的问题,也就迎刃而解了。

以上就是我对于两种不同权限访问的处理经验总结,希望给在Winform开发中的同行参考,感谢耐心的阅读和支持。
以上就介绍了 在Winform程序中设置管理员权限及为用户组添加写入权限,包括了方面的内容,希望对.NETjrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播有兴趣的朋友有所帮助。
本文网址链接:http://www.codes51.com/article/detail_2315414_2.html