rainit2006 / C-Program

C# Program knowledge
0 stars 0 forks source link

实际应用 #15

Open rainit2006 opened 7 years ago

rainit2006 commented 7 years ago

记录log到Debug Console Console.WriteLine("xxxxx" + var);

显示alert 对话框

MessageBox.Show("正しい値を入力してください。",
    "エラー",
    MessageBoxButtons.OK,
    MessageBoxIcon.Error);
rainit2006 commented 7 years ago

获得当前运行程序的名称:

//自分自身の実行ファイルのパスを取得する
string appPath = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;

其它的类似应用

//自分自身の実行ファイルのパスを取得する
string appPath = System.Reflection.Assembly.GetExecutingAssembly().Location;

//「C:\My Application\ClassLibrary1.dll」に書かれた以下のコードを
//「C:\My Application\Application.exe」から呼び出した時の結果をコメントに記す

Console.WriteLine(System.Reflection.Assembly.GetExecutingAssembly().Location);
//C:\My Application\ClassLibrary1.dll

Console.WriteLine(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
//file:///C:/My Application/ClassLibrary1.DLL

Console.WriteLine(System.Reflection.Assembly.GetExecutingAssembly().EscapedCodeBase);
//file:///C:/My%20Application/ClassLibrary1.DLL
rainit2006 commented 7 years ago

防止二重启动 C#

        System.Threading.Mutex mutex;

        public App()
        {
            this.Startup += new StartupEventHandler(App_Startup);
        }

        void App_Startup(object sender, StartupEventArgs e)
        {
            bool ret;
            string productName = System.IO.Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().GetName().Name);

            mutex = new System.Threading.Mutex(true, productName, out ret);
            if (!ret)
            {
                IntPtr CurrenthWnd = new IntPtr(0);

                CurrenthWnd = FindWindow(null, GetString.GetResourceFromTextDictionary("APP_WIN_TITLE"));
                if (CurrenthWnd != IntPtr.Zero)
                {
                    SendMessage(CurrenthWnd, WM_SYSCOMMAND, SC_RESTORE, 0);
                    SetForegroundWindow(CurrenthWnd);
                }

                Environment.Exit(0);
            }
            else
            {
                mutex.ReleaseMutex();  //ReleaseMutex并不会销毁Mutex,只是释放对Mutex的控制权。Mutex只有被调用mutex.Close时才会消失(或者程序退出时)
                CheckAdministrator();
                StartupUri = new Uri("MainWindow.xaml", UriKind.RelativeOrAbsolute);
            }
        }
rainit2006 commented 7 years ago

确认自己是否被高权限启动。如果不是则把自己以高权限启动

           var currentidentity = WindowsIdentity.GetCurrent();
            var principal = new WindowsPrincipal(currentidentity);

            if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
            {
                var processInfo = new ProcessStartInfo(Assembly.GetExecutingAssembly().CodeBase);

                processInfo.UseShellExecute = true;
                processInfo.Verb = "runas";

                try
                {
                    Process.Start(processInfo);
                }
                catch
                {                  
                }
                Environment.Exit(0);
            }
rainit2006 commented 6 years ago

http://www.geocities.jp/litorud/WPF.html