ホーム > タグ > C#
C#
[C#]アプリケーションのUAC対応
- 2011年4月17日 12:00 AM
- 資料
アプリケーション側で一般権限と管理者権限とで挙動を変えたい場合のメモ
- WindowsVista以降(UAC搭載OS)で起動されているか
アプリケーションがWindowsVista以降で起動されているかどうかをOSのバージョン情報から判断する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; } - アプリケーションが管理者権限で起動されているか
アプリケーションが「管理者として実行…」されているとき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; } - アプリケーションを起動したユーザーはAdministratorsグループに属しているか
アプリケーションは一般権限で起動されていたとしても起動したユーザーが管理者に昇格可能かどうか知りたいとき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; }上のコードだといくつかの環境でうまく動いていないっぽい?下のコードのほうが確実かも。
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; } - アプリケーションを管理者権限で再起動する
処理を行う際に管理権限が必要となった場合に自分自身を管理者権限で再起動する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(); } - 管理者権限が必要になるボタンにUAC(盾)のアイコンを表示する
操作の続行に管理者権限が必要になることをユーザーに知らせるためボタンにUAC(盾)のアイコンを表示する
(SystemIcons.Shieldプロパティの盾アイコンとWin32APIの盾アイコンには差があり、Windows 7で標準的に使われているのは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)); } } } }
ツイート
[C#]文字列整形関数
- 2010年11月8日 12:00 AM
- 記録
.Netframework 3.0 以降でなるだけ国・地域に依存しない感じで、文字の整形関数を考えてみた。
というのも、もともと用意されているPadLeftなどでは日本語の全角文字を使用するとずれてしまう。
ここでめんどくさいのが文字数と文字コードのバイト数ともに文字の表示数と一致しないこと。
全角文字はよく2バイト文字といわれていたが、Unicodeではそうならない。
表示上2バイト分(全角)でも、バイト数は3バイトとか4バイトも存在する。
もちろん、文字数でカウントしても全角文字は1文字として検出される。
つまり、3バイトの全角文字1文字をPadLeftで整形しようとしたときにめんどくささに気づくわけだ。
で、強引な方法で実装してみたのでメモ。
実際は、半角か全角かを検出できればいいから画面から取得なんて強引なことをしなくてもできそう。
でも、気がついた時にはすでに組み終わってたので、改良された方はトラックバックとかお願いします・・・。
//書式整形に使用するフォント
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);
}
ツイート
ホーム > タグ > C#
- 検索
- フィード
- 翻訳

