您好,登錄后才能下訂單哦!
這篇文章給大家介紹利用C#怎么對系統登錄用戶進行驗證,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
Windows用戶類型:Administrator、Guest、自定義用戶等;文件的權限不外乎:Read、Write、Modify、Remove等等,其中擁有所有權限的字符叫做FullControl。
C#當前程序用戶路徑權限驗證
/// <summary> /// 是否擁有程序數據路徑權限 /// </summary> private static bool HasProgramDataRights=false; public static bool IsAdministrator() { System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent(); System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity); return principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator); } /// <summary> /// 讀取系統程序數據路徑權限&判斷是否可以使用臨時目錄進行保存操作 /// </summary> /// <param name="path"></param> public void ReadProgramDataRights(string path) { // 通過判定文件的權限來控制路徑 System.Security.AccessControl.DirectorySecurity dirSec = new System.IO.DirectoryInfo(path).GetAccessControl(); System.Security.AccessControl.AuthorizationRuleCollection rules = dirSec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)); foreach (System.Security.AccessControl.FileSystemAccessRule rule in rules) { Console.WriteLine(path + "文件目錄權限字符:" + rule.FileSystemRights); // 判定文件是否具有FullControl或Write權限 if (rule.FileSystemRights.ToString().Equals("FullControl") || rule.FileSystemRights.ToString().Equals("Write")) { HasProgramDataRights = true; break; } } // 為IE保護模式創建臨時保存路徑 if (HasProgramDataRights) { // 創建操作路徑 string recordTemp = FileHelp.GetAppDataLocalLow() + "\\CvNetVideo\\Record\\"; string screenshotTemp = FileHelp.GetAppDataLocalLow() + "\\CvNetVideo\\Screenshot\\"; string recordDst = FileHelp.GetCommonApplicationData() + "\\CvNetVideo\\Record\\"; string screenshotDst = FileHelp.GetCommonApplicationData() + "\\CvNetVideo\\Screenshot\\"; Directory.CreateDirectory(recordTemp); Directory.CreateDirectory(screenshotTemp); Directory.CreateDirectory(recordDst); Directory.CreateDirectory(screenshotDst); } }
C#文件操作工具類
public class FileHelp { public enum GetDirectoryType { ByAppDomain, ByAssembly } public static string GetCurrentDirectory(GetDirectoryType type = GetDirectoryType.ByAppDomain) { switch (type) { case GetDirectoryType.ByAppDomain: return AppDomain.CurrentDomain.BaseDirectory; case GetDirectoryType.ByAssembly: return Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); default: return AppDomain.CurrentDomain.BaseDirectory; } } public static string GetCurrentDirectoryByAssembly() { return GetCurrentDirectory(GetDirectoryType.ByAssembly); } /// <summary> ///程序數據路徑- C:\ProgramData /// </summary> /// <returns></returns> public static string GetCommonApplicationData() { return Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); } /// <summary> /// 用戶數據路徑 /// </summary> /// <returns></returns> public static string GetApplicationData() { return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); } /// <summary> /// 用戶數據本地路徑 /// </summary> /// <returns></returns> public static string GetLocalApplicationData() { return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); } /// <summary> /// 用戶路徑 /// </summary> /// <returns></returns> public static string GetUserProfile() { return Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); } /// <summary> /// 用戶的圖片路徑 /// </summary> /// <returns></returns> public static string GetMyPictures() { return Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); } /// <summary> /// 用戶的視頻路徑 /// </summary> /// <returns></returns> public static string GetMyVideos() { return Environment.GetFolderPath(Environment.SpecialFolder.MyVideos); } /// <summary> /// 用戶的文檔路徑 /// </summary> /// <returns></returns> public static string GetMyDocuments() { return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); } /// <summary> /// IE保護模式下的低權限操作路徑(Temporary Internet Files/Low) /// </summary> /// <returns></returns> public static string GetTemporaryInternetFiles() { return GetLocalApplicationData()+ "\\Microsoft\\Windows\\Temporary Internet Files\\Low"; } /// <summary> /// IE保護模式下的低權限操作路徑(%userprofile%/AppData/LocalLow) /// </summary> /// <returns></returns> public static string GetAppDataLocalLow() { return GetUserProfile() + "\\AppData\\LocalLow"; } /// <summary> /// 獲取系統字體文件路徑 /// </summary> /// <returns></returns> public static string GetFonts() { return Environment.GetFolderPath(Environment.SpecialFolder.Fonts); } /// <summary> /// 二進制文件讀取 /// </summary> /// <param name="FileUrl">文件路徑</param> /// <returns></returns> public static byte[] BinaryRead(string FileUrl) { List<byte> lst = new List<byte>(); try { //文件路徑 String filename = FileUrl; //打開文件 FileStream FStream; if (File.Exists(filename)) { FStream = new FileStream(filename, FileMode.Open); } else { return null; } int BufferSize = 1048576; //每次讀取的字節數 每次讀取1MB byte[] Buffer = new byte[BufferSize]; long FileLength = FStream.Length;//文件流的長度 int ReadCount = (int)(FileLength / BufferSize) + 1; //需要對文件讀取的次數 //數據讀取 BinaryReader BWrite = new BinaryReader(FStream); //br.BaseStream.Seek(0, SeekOrigin.Begin); //while (br.BaseStream.Position < br.BaseStream.Length){} for (int a = 0; a < ReadCount; a++) { Buffer = BWrite.ReadBytes(BufferSize); lst.AddRange(Buffer); } BWrite.Close(); BWrite.Close(); return lst.ToArray(); } catch (System.Exception ex) { Log.WriteLog4Ex("FileHelp.BinaryRead", ex); return null; } } /// <summary> /// 二進制文件寫入 /// </summary> /// <param name="Bts"></param> /// <param name="DirectoryUrl">文件目錄路徑</param> /// <param name="FileName">文件名稱</param> /// <returns></returns> public static bool BinaryWrite(byte[] Bts, string DirectoryUrl, string FileName) { try { //文件路徑 string Filepath = DirectoryUrl + "\\" + FileName; //目錄創建 if (!Directory.Exists(DirectoryUrl)) Directory.CreateDirectory(DirectoryUrl); //文件創建 FileStream FStream; if (File.Exists(Filepath)) { FStream = new FileStream(Filepath, FileMode.Append); } else { FStream = new FileStream(Filepath, FileMode.Create); } //數據寫入 BinaryWriter BWrite = new BinaryWriter(FStream); BWrite.Write(Bts); BWrite.Close(); FStream.Close(); return true; } catch (System.Exception ex) { Log.WriteLog4Ex("FileHelp.BinaryWrite", ex); return false; } } /// <summary> /// 二進制文件刪除 /// </summary> /// <param name="FileUrl">文件路徑</param> public static void FileDelete(string FileUrl) { try { //文件路徑 String filename = FileUrl; //刪除文件 if (File.Exists(filename)) { File.Delete(filename); } } catch (System.Exception ex) { Log.WriteLog4Ex("FileHelp.FileDelete", ex); } } }
注:如果您是開發OCX遇到路徑問題請使用工具類中IE保護模式下的低權限路徑。
補充知識:C#修改文件或文件夾的權限,為指定用戶、用戶組添加完全控制權限
寫在前面
在windows系統中,c盤中的目錄權限比較高,有時制作安裝包的時候,默認的安裝路徑就是在c盤,但對運行可執行文件,有時候需要為其添加完全控制權限,或者讀寫權限。這里將當時的解決方案記錄一下。
代碼實現
在C盤添加一個文件夾,并在文件夾內部,新建一個文本文件,如圖所示:
該文件夾下,新建一個文本文件,如圖所示:
為文件添加完全控制權限:
/// <summary> /// 為文件添加users,everyone用戶組的完全控制權限 /// </summary> /// <param name="filePath"></param> static void AddSecurityControll2File(string filePath) { //獲取文件信息 FileInfo fileInfo = new FileInfo(filePath); //獲得該文件的訪問權限 System.Security.AccessControl.FileSecurity fileSecurity = fileInfo.GetAccessControl(); //添加ereryone用戶組的訪問權限規則 完全控制權限 fileSecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow)); //添加Users用戶組的訪問權限規則 完全控制權限 fileSecurity.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow)); //設置訪問權限 fileInfo.SetAccessControl(fileSecurity); }
為文件夾添加完全控制權限
/// <summary> ///為文件夾添加users,everyone用戶組的完全控制權限 /// </summary> /// <param name="dirPath"></param> static void AddSecurityControll2Folder(string dirPath) { //獲取文件夾信息 DirectoryInfo dir = new DirectoryInfo(dirPath); //獲得該文件夾的所有訪問權限 System.Security.AccessControl.DirectorySecurity dirSecurity = dir.GetAccessControl(AccessControlSections.All); //設定文件ACL繼承 InheritanceFlags inherits = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit; //添加ereryone用戶組的訪問權限規則 完全控制權限 FileSystemAccessRule everyoneFileSystemAccessRule = new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, inherits, PropagationFlags.None, AccessControlType.Allow); //添加Users用戶組的訪問權限規則 完全控制權限 FileSystemAccessRule usersFileSystemAccessRule = new FileSystemAccessRule("Users", FileSystemRights.FullControl, inherits, PropagationFlags.None, AccessControlType.Allow); bool isModified = false; dirSecurity.ModifyAccessRule(AccessControlModification.Add, everyoneFileSystemAccessRule, out isModified); dirSecurity.ModifyAccessRule(AccessControlModification.Add, usersFileSystemAccessRule, out isModified); //設置訪問權限 dir.SetAccessControl(dirSecurity); }
關于利用C#怎么對系統登錄用戶進行驗證就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。