Home > Tags> C #

C #

UAC corresponding [C #] application

Note If you want to change the behavior in the administrative privileges and general authority on the application side

  1. I judge from the version information of the OS <br /> whether the application has been started in WindowsVista or later has been started in (UAC installed OS) WindowsVista or later

    using System; private bool IsUAC() { OperatingSystem osInfo = Environment.OSVersion; if (osInfo.Platform == PlatformID.Win32NT) { if (osInfo.Version.Major == 6) { if (osInfo.Version.Minor == 0) { // Windows Vista, Windows Server 2008 return true; } else if (osInfo.Version.Minor == 1) { // Windows 7, Windows Server 2008 R2 return true; } } else if (osInfo.Version.Major > 6) { // new Windows return true; } } return false; } 
  2. When <br /> application is "Run as administrator" whether the application has been started with administrator privileges

     using System.Security.Principal; private bool IsAdministrator() { bool isAllowed = false; try { WindowsIdentity wi = WindowsIdentity.GetCurrent(); WindowsPrincipal wp = new WindowsPrincipal(wi); isAllowed = wp.IsInRole(WindowsBuiltInRole.Administrator); } catch { isAllowed = false; } return isAllowed; } 
  3. The user who started the application when you want to know the user who started even have been started by the general authority whether promoted to the administrator <br /> application belongs to the Administrators group

     using System.DirectoryServices.AccountManagement; private bool IsAdministrators() { bool isAllowed = false; try { PrincipalContext pc = new PrincipalContext(ContextType.Machine, null); UserPrincipal up = UserPrincipal.Current; GroupPrincipal gp = GroupPrincipal.FindByIdentity(pc, "Administrators"); if (up.IsMemberOf(gp)) { isAllowed = true; } } catch { isAllowed = false; } return isAllowed; } 

    Ish not working well in some environments it's the above code? More of the code below is be sure.

     using System.DirectoryServices.AccountManagement; private bool IsAdministrators() { bool isAllowed = false; try { PrincipalContext pc = new PrincipalContext(ContextType.Machine, null); UserPrincipal up = UserPrincipal.Current; foreach (GroupPrincipal gp in up.GetGroups()) { try { if (gp.Name.ToLower().Equals("Administrators".ToLower())) { isAllowed = true; } } finally { gp.Dispose(); } } } catch { isAllowed = false; } return isAllowed; } 
  4. Restart with administrator privileges themselves in case it becomes necessary to administrative privileges when performing <br /> process to restart with administrator privileges the application

     using System; using System.Diagnostics; private void RestartApplication() { ProcessStartInfo psi = new ProcessStartInfo(); psi.UseShellExecute = true; psi.WorkingDirectory = Environment.CurrentDirectory; psi.FileName = Application.ExecutablePath; psi.Verb = "runas"; try { Process p = Process.Start(psi); } catch { return; } Application.Exit(); } 
  5. Show icon UAC (vertical) to the button to inform the user that need administrator privileges to continue <br /> operation that displays the icon UAC (vertical) to the button that require administrative privileges (There is a difference in the shield icon in the Win32API and shield icon of SystemIcons.Shield property, it's being used as a standard in Windows 7 is. is more of Win32 API)

     using System; using System.Windows.Forms; using System.Runtime.InteropServices; namespace Sample { public partial class Form1 : Form { [DllImport("user32.dll")] private static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam); // 第2パラメータ:盾アイコンを設定するフラグ uint BCM_SETSHIELD = 0x0000160C; public Form1() { InitializeComponent(); // ボタンの外観を「System」にする必要がある button1.FlatStyle = FlatStyle.System; } private void checkBox1_CheckedChanged(object sender, EventArgs e) { // 第1パラメータ:ボタンのウィンドウ・ハンドル HandleRef hwnd = new HandleRef(button1, button1.Handle); if (checkBox1.Checked) { SendMessage(hwnd, BCM_SETSHIELD, new IntPtr(0), new IntPtr(1)); } else { SendMessage(hwnd, BCM_SETSHIELD, new IntPtr(0), new IntPtr(0)); } } } } 
ツイート Tweets

[C #] string formatting function

Feeling that it is not dependent on the country and region and only made of .Netframework 3.0 or later, I tried to think of formatting functions of character.
Because, it deviates when you use double-byte Japanese characters such as PadLeft you are prepared originally.
That does not match the display number of characters in both number of bytes in a character code and number of characters that can be troublesome for here.

Double-byte characters had been referred to as double-byte character well, but it does not happen in Unicode.
2 bytes on display but (em), the number of bytes is also present three or four bytes bytes.
Double-byte characters are detected as one character, of course, can be counted in characters.
In other words, it is a translation notice Mendokusa is when you try to PadLeft shaped by a single character byte characters of 3 bytes.

At, notes because I tried to implemented in a pushy way.
In fact, it is likely to be without that aggressive Nante retrieved from the screen because only to be capable of detecting whether or full-width half.
But, because he finished already assembled when I noticed, ... thank you Toka trackback Those who have been improved.

 //書式整形に使用するフォントpublic Font fixedPitchFont; public setFixedPitchFont() { //書式整形に使用するフォントをユーザー環境に合わせて切り替える System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture; if (ci.TextInfo.LCID == 1041) { //日本 fixedPitchFont = new Font("MS ゴシック", SystemFonts.DefaultFont.SizeInPoints); } else { //その他("Courier New":マルチバイト時にずれる) fixedPitchFont = new Font(FontFamily.GenericMonospace, SystemFonts.DefaultFont.SizeInPoints); } } private void GetFontPitch() { //描画サイズから1文字表示分の幅を取得する // 1. DPIから求める { //解像度の取得 Font font20pixel = new Font(FontFamily.GenericMonospace, 20, GraphicsUnit.Pixel); int dpi = (int)Math.Round((72 * font20pixel.Size) / font20pixel.SizeInPoints); //固定幅はWidth:Height=1:2で作成されていることを前提として fixedPitchFontHeight = (int)Math.Ceiling(fixedPitchFont.SizeInPoints * dpi / 72); fixedPitchFontWidth = fixedPitchFontHeight / 2; } // 2. 念のため、描画してみた大きさから求めたものと比較する { //適当な大きさで描画用Bmpを作成(メインディスプレイの大きさで作成) Bitmap bmp = new Bitmap(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height); try { Graphics g = Graphics.FromImage(bmp); Size Size1word = TextRenderer.MeasureText(g, " ", fixedPitchFont); //半角(1Word) Size Size2word = TextRenderer.MeasureText(g, "m1", fixedPitchFont); //全角(マルチバイト文字も表示上2Word) int wordWidth = Size2byte.Width - Size1byte.Width; int wordHeight = Size1byte.Height; //一致しない場合は固定幅フォントではない可能性がある。とりあえず大きい方を使う。 if (fixedPitchFontWidth != wordWidth) { fixedPitchFontWidth = Math.Max(fixedPitchFontWidth, wordWidth); } if (fixedPitchFontWidth != wordWidth) { fixedPitchFontHeight = Math.Max(fixedPitchFontHeight, wordHeight); } } finally { bmp.Dispose(); } } } public int Display1ByteWordCount(string text) { int displayFontWidth; //適当な大きさで描画用Bmpを作成(メインディスプレイの大きさで作成) Bitmap bmp = new Bitmap(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height); Graphics g = Graphics.FromImage(bmp); Size stringSize = TextRenderer.MeasureText(g, text, fixedPitchFont); //表示に使用したpixelから文字数を逆算する。 displayFontWidth = stringSize.Width / fixedPitchFontWidth + (int)Math.Round((double)(stringSize.Width % fixedPitchFontWidth) / 10); return displayFontWidth; } public string PaddingLeft(string text, int totalWidth) { int diff = Display1ByteWordCount(text) - text.Length; return text.PadLeft(totalWidth - diff); } public string PaddingRight(string text, int totalWidth) { int diff = Display1ByteWordCount(text) - text.Length; return text.PadRight(totalWidth - diff); } 
ツイート Tweets

Home > Tags> C #

Search
Feed
Translation
Japanese flagEnglish flag

I return to the top of the page