顯示具有 C# 標籤的文章。 顯示所有文章
顯示具有 C# 標籤的文章。 顯示所有文章

2024年4月7日 星期日

[研究]C# 列出所有使用者的所有程序,及每個程序的執行者

[研究]C# 列出所有使用者的所有程序,及每個程序的執行者

2024-04-07

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Management;

class Program
{
     static void Main(string[] args)
     {
         // 取得本機電腦上所有行程
         Process[] processes = Process.GetProcesses();

         // 建立字典來儲存每個使用者及其進程
         Dictionary<string, List<Process>> userProcesses = new Dictionary<string, List<Process>>();

         // 遍歷每個行程
         foreach (Process process in processes)
         {
             try
             {
                 string owner = GetProcessOwner(process);

                 // 如果用戶不存在於字典中,新增用戶
                 if (!userProcesses.ContainsKey(owner))
                 {
                     userProcesses.Add(owner, new List<Process>());
                 }

                 // 新增進程到使用者的進程清單中
                 userProcesses[owner].Add(process);
             }
             catch (Exception ex)
             {
                 // 忽略無法存取進程資訊的異常
                 Console.WriteLine($"Error retrieving information for process {process.ProcessName}: {ex.Message}");
             }
         }

         // 輸出每個使用者的行程
         foreach (var user in userProcesses)
         {
             Console.WriteLine($"User: {user.Key}");
             foreach (var process in user.Value)
             {
                 Console.WriteLine($" 程序名稱: {process.ProcessName}, 執行者: {GetProcessOwner(process)}");
             }
             Console.WriteLine();
         }

         // 提示使用者按下任意鍵以繼續
         Console.WriteLine("按下任意鍵退出程式...");
         Console.ReadKey();
     }

     // 取得進程的擁有者
     static string GetProcessOwner(Process process)
     {
         string query = "Select * From Win32_Process Where ProcessID = " + process.Id;
         ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
         ManagementObjectCollection processList = searcher.Get();

         foreach (ManagementObject obj in processList)
         {
             string[] argList = new string[] { string.Empty, string.Empty };
             int returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList));
             if (returnVal == 0)
             {
                 // 傳回進程擁有者
                 return argList[0];
             }
         }

         return "Unknown";
     }
}


上面程式實際測試可以跑,但執行很慢。

********************************************************************************

只顯示process,其實很快。

using System;
using System.Diagnostics;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {

            Process[] processlist = Process.GetProcesses();

            foreach (Process theprocess in processlist)
            {
                Console.WriteLine("Process: {0} ID: {1}", theprocess.ProcessName, theprocess.Id);
            }
            Console.ReadKey();
        }
    }
}

執行結果:(很快)

Process: sqlservr ID: 16540
Process: svchost ID: 3240
Process: svchost ID: 4588
Process: svchost ID: 9108
Process: conhost ID: 11016
Process: taskhostw ID: 12240
Process: svchost ID: 1824
Process: chrome ID: 19284
Process: ServiceHub.RoslynCodeAnalysisService ID: 14796
Process: svchost ID: 3656
Process: fontdrvhost ID: 22644
Process: Microsoft.ServiceHub.Controller ID: 24912
Process: hvsirpcd ID: 2728
Process: svchost ID: 1800
Process: cmd ID: 11904
Process: svchost ID: 3632
Process: svchost ID: 2712
Process: svchost ID: 8224
Process: wlanext ID: 4544
Process: svchost ID: 2704
Process: RuntimeBroker ID: 14648
Process: svchost ID: 8212
Process: vmwp ID: 6372
Process: chrome ID: 24748
Process: SearchProtocolHost ID: 6364
Process: svchost ID: 11872
Process: conhost ID: 7276
Process: chrome ID: 14624
Process: SpotifyWidgetProvider ID: 2676
Process: msedge ID: 23812
Process: vmware-authd ID: 21972
Process: RuntimeBroker ID: 11860
Process: ConsoleApp1 ID: 14760
Process: OpenConsole ID: 7260
Process: csrss ID: 824
Process: igfxEM ID: 10012
Process: svchost ID: 7252
Process: svchost ID: 1632
Process: chrome ID: 14600
Process: svchost ID: 6328
Process: svchost ID: 4580
Process: SystemSettings ID: 7240
Process: svchost ID: 4480
Process: Notepad ID: 14588
Process: msedgewebview2 ID: 15504
Process: explorer ID: 17180
Process: msedgewebview2 ID: 21012
Process: svchost ID: 1712
Process: conhost ID: 18252
Process: svchost ID: 1704
Process: svchost ID: 3536
Process: chrome ID: 11800
Process: ctfmon ID: 8120
Process: svchost ID: 4440
Process: svchost ID: 8112
Process: chrome ID: 12704
Process: PhoneExperienceHost ID: 18216
Process: svchost ID: 16376
Process: svchost ID: 1668
Process: svchost ID: 11992
Process: vmnetdhcp ID: 12692
Process: OneApp.IGCC.WinService ID: 5336
Process: RuntimeBroker ID: 17280
Process: vmwp ID: 1656
Process: chrome ID: 14520
Process: RtkAudUService64 ID: 19112
Process: SynTPHelper ID: 17272
Process: msedge ID: 23704
Process: RuntimeBroker ID: 26456
Process: svchost ID: 3480
Process: svchost ID: 1640
Process: spoolsv ID: 4392
Process: svchost ID: 3472
Process: SecurityHealthSystray ID: 9904
Process: svchost ID: 1996
Process: svchost ID: 5056
Process: dwm ID: 8404
Process: svchost ID: 1624
Process: ServiceHub.ThreadedWaitDialog ID: 11732
Process: svchost ID: 21840
Process: svchost ID: 16324
Process: msedgewebview2 ID: 17240
Process: svchost ID: 11724
Process: msedgewebview2 ID: 7128
Process: chrome ID: 24588
Process: msedge ID: 10800
Process: igfxCUIService ID: 3444
Process: svchost ID: 2524
Process: LsaIso ID: 676
Process: svchost ID: 9864
Process: TextInputHost ID: 19052
Process: svchost ID: 8940
Process: OneDrive ID: 20884
Process: IntelliTrace ID: 8016
Process: hvsirpcd ID: 17204
Process: svchost ID: 12608
Process: Widgets ID: 10768
Process: services ID: 652
Process: DSDFunctionKeyCtlService ID: 7996
Process: msedge ID: 2480
Process: svchost ID: 8912
Process: svchost ID: 21776
Process: StartMenuExperienceHost ID: 2476
Process: svchost ID: 19932
Process: ShellExperienceHost ID: 14412
Process: svchost ID: 9812
Process: vmmemMDAG ID: 16244
Process: vmnat ID: 22676
Process: svchost ID: 23532
Process: vmmemMDAG ID: 21752
Process: inetinfo ID: 5208
Process: chrome ID: 2688
Process: BingSvc ID: 4092
Process: RuntimeBroker ID: 20824
Process: svchost ID: 7512
Process: winlogon ID: 22660
Process: SecurityHealthSystray ID: 6116
Process: NisSrv ID: 11616
Process: gamingservicesnet ID: 7952
Process: svchost ID: 13464
Process: svchost ID: 18972
Process: svchost ID: 1508
Process: smss ID: 588
Process: hvsimgr ID: 9776
Process: dllhost ID: 12532
Process: WUDFHost ID: 3340
Process: UserOOBEBroker ID: 9772
Process: svchost ID: 1500
Process: gamingservices ID: 7932
Process: splwow64 ID: 2416
Process: svchost ID: 11604
Process: ServiceHub.SettingsHost ID: 22628
Process: SystemSettingsBroker ID: 25380
Process: svchost ID: 12504
Process: IGCC ID: 18016
Process: svchost ID: 4228
Process: conhost ID: 10660
Process: dllhost ID: 3300
Process: svchost ID: 17996
Process: svchost ID: 5128
Process: svchost ID: 15236
Process: svchost ID: 3288
Process: TextInputHost ID: 14308
Process: msedge ID: 24412
Process: msedgewebview2 ID: 18892
Process: lsass ID: 860
Process: dwm ID: 12456
Process: svchost ID: 2444
Process: svchost ID: 3264
Process: svchost ID: 4280
Process: ctfmon ID: 17044
Process: msedgewebview2 ID: 9896
Process: backgroundTaskHost ID: 23472
Process: svchost ID: 7848
Process: sihost ID: 10600
Process: msedge ID: 14184
Process: SearchHost ID: 16112
Process: svchost ID: 17944
Process: WindowsTerminal ID: 26600
Process: msedgewebview2 ID: 14264
Process: svchost ID: 4152
Process: svchost ID: 5068
Process: SearchIndexer ID: 21600
Process: RuntimeBroker ID: 23436
Process: hvsirdpclient ID: 2296
Process: WidgetService ID: 4132
Process: igfxEM ID: 9644
Process: svchost ID: 8720
Process: AggregatorHost ID: 7800
Process: msedgewebview2 ID: 6880
Process: svchost ID: 17904
Process: svchost ID: 16052
Process: hvsirdpclient ID: 11452
Process: hvsimgr ID: 23396
Process: svchost ID: 2256
Process: csrss ID: 15120
Process: cmd ID: 22464
Process: LockApp ID: 14192
Process: backgroundTaskHost ID: 9596
Process: svchost ID: 1324
Process: svchost ID: 4080
Process: msedge ID: 17864
Process: SynTPEnh ID: 21536
Process: vmware-usbarbitrator64 ID: 20616
Process: RuntimeBroker ID: 11424
Process: vmmemCmZygote ID: 13260
Process: IntelCpHeciSvc ID: 2212
Process: dynabookSystemService ID: 4968
Process: svchost ID: 10480
Process: PerfWatson2 ID: 20584
Process: fontdrvhost ID: 15068
Process: picpick ID: 14148
Process: svchost ID: 20580
Process: svchost ID: 1276
Process: OpenConsole ID: 21488
Process: svchost ID: 4944
Process: DSDFunctionKeyCtlService ID: 18728
Process: LogonUI ID: 7688
Process: chrome ID: 14116
Process: svchost ID: 16872
Process: svchost ID: 8600
Process: smartscreen ID: 22384
Process: svchost ID: 4920
Process: ONENOTEM ID: 4000
Process: GoogleCrashHandler64 ID: 9512
Process: msedge ID: 19620
Process: svchost ID: 4912
Process: vmware-tray ID: 19248
Process: GoogleCrashHandler ID: 9504
Process: svchost ID: 3064
Process: svchost ID: 1056
Process: WidgetService ID: 12252
Process: svchost ID: 2460
Process: svchost ID: 4296
Process: taskhostw ID: 8568
Process: svchost ID: 4892
Process: SecurityHealthService ID: 3972
Process: svchost ID: 19592
Process: msedgewebview2 ID: 24184
Process: DSDFunctionKeyCtlService ID: 4884
Process: RuntimeBroker ID: 17832
Process: msedge ID: 3960
Process: svchost ID: 4876
Process: chrome ID: 23252
Process: msedgewebview2 ID: 19492
Process: svchost ID: 2112
Process: svchost ID: 4868
Process: svchost ID: 18652
Process: svchost ID: 16812
Process: SynTPHelper ID: 19568
Process: Video.UI ID: 17728
Process: MsMpEng ID: 4856
Process: svchost ID: 10364
Process: WMIRegistrationService ID: 4848
Process: svchost ID: 3924
Process: svchost ID: 3000
Process: PerfWatson2 ID: 13108
Process: vmcompute ID: 4836
Process: svchost ID: 2076
Process: svchost ID: 12184
Process: conhost ID: 4828
Process: chrome ID: 25044
Process: fontdrvhost ID: 1144
Process: DtsApo4Service ID: 4812
Process: dllhost ID: 14920
Process: RuntimeBroker ID: 23184
Process: ServiceHub.DataWarehouseHost ID: 11236
Process: rundll32 ID: 2964
Process: svchost ID: 2044
Process: svchost ID: 3880
Process: chrome ID: 17664
Process: svchost ID: 1116
Process: chrome ID: 7548
Process: RtkAudUService64 ID: 4788
Process: svchost ID: 3860
Process: RtkAudUService64 ID: 15804
Process: svchost ID: 2016
Process: RMService ID: 4772
Process: svchost ID: 2932
Process: LocationNotificationWindows ID: 24064
Process: sqlwriter ID: 4764
Process: svchost ID: 2924
Process: StandardCollector.Service ID: 5680
Process: OneDrive ID: 23140
Process: Widgets ID: 14868
Process: sihost ID: 24056
Process: Registry ID: 160
Process: chrome ID: 5672
Process: WmiPrvSE ID: 11184
Process: svchost ID: 2912
Process: WmiPrvSE ID: 13016
Process: svchost ID: 19436
Process: svchost ID: 1052
Process: csrss ID: 2888
Process: chrome ID: 12996
Process: ServiceHub.VSDetouredHost ID: 1964
Process: ServiceHub.Host.CLR.x86 ID: 15744
Process: msedgewebview2 ID: 22176
Process: msedgewebview2 ID: 2876
Process: RuntimeBroker ID: 3792
Process: ShellExperienceHost ID: 16656
Process: OfficeClickToRun ID: 15848
Process: svchost ID: 24004
Process: svchost ID: 4704
Process: Secure System ID: 108
Process: RuntimeBroker ID: 4700
Process: IntelCpHDCPSvc ID: 2008
Process: Memory Compression ID: 3160
Process: RuntimeBroker ID: 15720
Process: svchost ID: 11124
Process: dllhost ID: 13880
Process: svchost ID: 8164
Process: explorer ID: 8364
Process: msedge ID: 3768
Process: SynTPEnh ID: 17552
Process: LocationNotificationWindows ID: 20308
Process: svchost ID: 7440
Process: devenv ID: 18784
Process: ApplicationFrameHost ID: 23608
Process: svchost ID: 9252
Process: svchost ID: 5572
Process: chrome ID: 4652
Process: vmware-tray ID: 22112
Process: ServiceHub.IdentityHost ID: 19352
Process: StartMenuExperienceHost ID: 16592
Process: winlogon ID: 15668
Process: OpenConsole ID: 23936
Process: svchost ID: 14744
Process: SearchHost ID: 21176
Process: svchost ID: 4632
Process: SynTPEnhService ID: 2792
Process: jhi_service ID: 5544
Process: svchost ID: 4620
Process: svchost ID: 1860
Process: msedge ID: 12880
Process: RuntimeBroker ID: 22988
Process: svchost ID: 3684
Process: RuntimeBroker ID: 2764
Process: wininit ID: 924
Process: System ID: 4
Process: svchost ID: 4596
Process: Idle ID: 0

********************************************************************************

在 C# 中,如果不使用 ManagementObjectSearcher 類別,則無法直接獲取進程的使用者名稱。這是因為 .NET 框架並未提供直接的 API 來實現這一功能。

但是,我們可以通過調用 Windows 的 tasklist 命令並解析其輸出來獲取進程的使用者名稱;或使用 Win32 SDK。

using System;
using System.Diagnostics;
using System.IO;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            ProcessStartInfo psi = new ProcessStartInfo("tasklist", "/v /fo csv")
            {
                RedirectStandardOutput = true,
                UseShellExecute = false,
                CreateNoWindow = true
            };

            Process process = Process.Start(psi);
            StreamReader reader = process.StandardOutput;
            string output = reader.ReadToEnd();

            string[] lines = output.Split('\n');
            foreach (string line in lines)
            {
                string[] fields = line.Split(',');
                if (fields.Length > 2)
                {
                    string processName = fields[0].Trim('"');
                    string processId = fields[1].Trim('"');
                    string username = fields[7].Trim('"');

                    Console.WriteLine($"Process: {processName} ID: {processId} User: {username}");
                }
            }

            Console.ReadKey();
        }
    }
} 

執行結果 (很慢)

Process: 映像名稱 ID: PID User: CPU 時間
Process: System Idle Process ID: 0 User: 974:07:49
Process: System ID: 4 User: 0:49:24
Process: Secure System ID: 108 User: 不適用
Process: Registry ID: 160 User: 不適用
Process: smss.exe ID: 588 User: 不適用
Process: csrss.exe ID: 824 User: 不適用
Process: wininit.exe ID: 924 User: 不適用
Process: services.exe ID: 652 User: 不適用
Process: LsaIso.exe ID: 676 User: 不適用
Process: lsass.exe ID: 860 User: 不適用
Process: svchost.exe ID: 1116 User: 不適用
Process: fontdrvhost.exe ID: 1144 User: 不適用
Process: svchost.exe ID: 1276 User: 不適用
Process: svchost.exe ID: 1324 User: 不適用
Process: svchost.exe ID: 1500 User: 不適用
Process: svchost.exe ID: 1508 User: 不適用
Process: svchost.exe ID: 1624 User: 不適用
Process: svchost.exe ID: 1632 User: 不適用
Process: svchost.exe ID: 1640 User: 不適用
Process: svchost.exe ID: 1704 User: 不適用
Process: svchost.exe ID: 1712 User: 不適用
Process: svchost.exe ID: 1800 User: 不適用
Process: svchost.exe ID: 1824 User: 不適用
Process: svchost.exe ID: 1860 User: 不適用
Process: IntelCpHDCPSvc.exe ID: 2008 User: 不適用
Process: svchost.exe ID: 2016 User: 不適用
Process: svchost.exe ID: 2044 User: 不適用
Process: svchost.exe ID: 1056 User: 不適用
Process: svchost.exe ID: 1052 User: 不適用
Process: svchost.exe ID: 1996 User: 不適用
Process: IntelCpHeciSvc.exe ID: 2212 User: 不適用
Process: svchost.exe ID: 2256 User: 不適用
Process: svchost.exe ID: 2444 User: 不適用
Process: svchost.exe ID: 2704 User: 不適用
Process: svchost.exe ID: 2712 User: 不適用
Process: SynTPEnhService.exe ID: 2792 User: 不適用
Process: svchost.exe ID: 2912 User: 不適用
Process: svchost.exe ID: 2924 User: 不適用
Process: svchost.exe ID: 2932 User: 不適用
Process: svchost.exe ID: 3000 User: 不適用
Process: svchost.exe ID: 3064 User: 不適用
Process: svchost.exe ID: 2076 User: 不適用
Process: svchost.exe ID: 2460 User: 不適用
Process: svchost.exe ID: 2524 User: 不適用
Process: Memory Compression ID: 3160 User: 不適用
Process: svchost.exe ID: 3240 User: 不適用
Process: svchost.exe ID: 3264 User: 不適用
Process: svchost.exe ID: 3288 User: 不適用
Process: WUDFHost.exe ID: 3340 User: 不適用
Process: igfxCUIService.exe ID: 3444 User: 不適用
Process: svchost.exe ID: 3472 User: 不適用
Process: svchost.exe ID: 3480 User: 不適用
Process: svchost.exe ID: 3656 User: 不適用
Process: svchost.exe ID: 3684 User: 不適用
Process: svchost.exe ID: 3860 User: 不適用
Process: svchost.exe ID: 3880 User: 不適用
Process: svchost.exe ID: 3924 User: 不適用
Process: svchost.exe ID: 3536 User: 不適用
Process: svchost.exe ID: 4152 User: 不適用
Process: svchost.exe ID: 4228 User: 不適用
Process: svchost.exe ID: 4280 User: 不適用
Process: svchost.exe ID: 4296 User: 不適用
Process: spoolsv.exe ID: 4392 User: 不適用
Process: svchost.exe ID: 4480 User: 不適用
Process: wlanext.exe ID: 4544 User: 不適用
Process: svchost.exe ID: 4580 User: 不適用
Process: svchost.exe ID: 4588 User: 不適用
Process: svchost.exe ID: 4596 User: 不適用
Process: svchost.exe ID: 4620 User: 不適用
Process: svchost.exe ID: 4632 User: 不適用
Process: sqlwriter.exe ID: 4764 User: 不適用
Process: RMService.exe ID: 4772 User: 不適用
Process: RtkAudUService64.exe ID: 4788 User: 不適用
Process: DtsApo4Service.exe ID: 4812 User: 不適用
Process: conhost.exe ID: 4828 User: 不適用
Process: vmcompute.exe ID: 4836 User: 不適用
Process: WMIRegistrationService.exe ID: 4848 User: 不適用
Process: MsMpEng.exe ID: 4856 User: 不適用
Process: svchost.exe ID: 4868 User: 不適用
Process: svchost.exe ID: 4876 User: 不適用
Process: DSDFunctionKeyCtlService.exe ID: 4884 User: 不適用
Process: svchost.exe ID: 4892 User: 不適用
Process: svchost.exe ID: 4912 User: 不適用
Process: svchost.exe ID: 4944 User: 不適用
Process: dynabookSystemService.exe ID: 4968 User: 不適用
Process: svchost.exe ID: 5056 User: 不適用
Process: svchost.exe ID: 5068 User: 不適用
Process: inetinfo.exe ID: 5208 User: 不適用
Process: OneApp.IGCC.WinService.exe ID: 5336 User: 不適用
Process: jhi_service.exe ID: 5544 User: 不適用
Process: svchost.exe ID: 5572 User: 不適用
Process: svchost.exe ID: 7252 User: 不適用
Process: gamingservices.exe ID: 7932 User: 不適用
Process: gamingservicesnet.exe ID: 7952 User: 不適用
Process: svchost.exe ID: 8164 User: 不適用
Process: svchost.exe ID: 7512 User: 不適用
Process: AggregatorHost.exe ID: 7800 User: 不適用
Process: svchost.exe ID: 4704 User: 不適用
Process: svchost.exe ID: 7440 User: 不適用
Process: svchost.exe ID: 8600 User: 不適用
Process: svchost.exe ID: 8720 User: 不適用
Process: svchost.exe ID: 8912 User: 不適用
Process: svchost.exe ID: 9108 User: 不適用
Process: svchost.exe ID: 9252 User: 不適用
Process: GoogleCrashHandler.exe ID: 9504 User: 不適用
Process: GoogleCrashHandler64.exe ID: 9512 User: 不適用
Process: svchost.exe ID: 10364 User: 不適用
Process: NisSrv.exe ID: 11616 User: 不適用
Process: svchost.exe ID: 11872 User: 不適用
Process: svchost.exe ID: 11724 User: 不適用
Process: SecurityHealthService.exe ID: 3972 User: 不適用
Process: vmmemCmZygote ID: 13260 User: 0:00:00
Process: svchost.exe ID: 11124 User: 不適用
Process: svchost.exe ID: 7848 User: 不適用
Process: svchost.exe ID: 11604 User: 不適用
Process: svchost.exe ID: 4080 User: 不適用
Process: svchost.exe ID: 3632 User: 不適用
Process: svchost.exe ID: 4920 User: 不適用
Process: svchost.exe ID: 8212 User: 不適用
Process: svchost.exe ID: 8224 User: 不適用
Process: svchost.exe ID: 2112 User: 不適用
Process: svchost.exe ID: 1668 User: 不適用
Process: svchost.exe ID: 9812 User: 不適用
Process: svchost.exe ID: 10480 User: 不適用
Process: svchost.exe ID: 14744 User: 不適用
Process: sqlservr.exe ID: 16540 User: 不適用
Process: svchost.exe ID: 21776 User: 不適用
Process: svchost.exe ID: 21840 User: 不適用
Process: vmnat.exe ID: 22676 User: 不適用
Process: vmnetdhcp.exe ID: 12692 User: 不適用
Process: svchost.exe ID: 15236 User: 不適用
Process: vmware-usbarbitrator64.exe ID: 20616 User: 不適用
Process: vmware-authd.exe ID: 21972 User: 不適用
Process: svchost.exe ID: 17996 User: 不適用
Process: OfficeClickToRun.exe ID: 15848 User: 不適用
Process: SearchIndexer.exe ID: 21600 User: 不適用
Process: svchost.exe ID: 4440 User: 不適用
Process: svchost.exe ID: 19932 User: 不適用
Process: svchost.exe ID: 19436 User: 不適用
Process: csrss.exe ID: 2888 User: 不適用
Process: winlogon.exe ID: 15668 User: 不適用
Process: fontdrvhost.exe ID: 15068 User: 不適用
Process: LogonUI.exe ID: 7688 User: 不適用
Process: dwm.exe ID: 8404 User: 不適用
Process: DSDFunctionKeyCtlService.exe ID: 18728 User: 不適用
Process: sihost.exe ID: 10600 User: 不適用
Process: svchost.exe ID: 20580 User: 不適用
Process: svchost.exe ID: 13464 User: 不適用
Process: svchost.exe ID: 6328 User: 不適用
Process: igfxEM.exe ID: 10012 User: 不適用
Process: svchost.exe ID: 8940 User: 不適用
Process: taskhostw.exe ID: 8568 User: 不適用
Process: explorer.exe ID: 8364 User: 不適用
Process: vmwp.exe ID: 1656 User: 不適用
Process: vmmemMDAG ID: 21752 User: 不適用
Process: SynTPEnh.exe ID: 17552 User: 不適用
Process: svchost.exe ID: 9864 User: 不適用
Process: SynTPHelper.exe ID: 19568 User: 不適用
Process: SearchHost.exe ID: 16112 User: 不適用
Process: StartMenuExperienceHost.exe ID: 2476 User: 不適用
Process: Widgets.exe ID: 14868 User: 不適用
Process: RuntimeBroker.exe ID: 2764 User: 不適用
Process: RuntimeBroker.exe ID: 23436 User: 不適用
Process: svchost.exe ID: 16872 User: 不適用
Process: dllhost.exe ID: 13880 User: 不適用
Process: ctfmon.exe ID: 8120 User: 不適用
Process: svchost.exe ID: 24004 User: 不適用
Process: hvsimgr.exe ID: 9776 User: 不適用
Process: hvsirpcd.exe ID: 2728 User: 不適用
Process: hvsirdpclient.exe ID: 11452 User: 不適用
Process: RuntimeBroker.exe ID: 22988 User: 不適用
Process: TextInputHost.exe ID: 14308 User: 不適用
Process: RtkAudUService64.exe ID: 19112 User: 不適用
Process: SecurityHealthSystray.exe ID: 6116 User: 不適用
Process: LocationNotificationWindows.exe ID: 24064 User: 不適用
Process: rundll32.exe ID: 2964 User: 不適用
Process: OneDrive.exe ID: 20884 User: 不適用
Process: backgroundTaskHost.exe ID: 9596 User: 不適用
Process: RuntimeBroker.exe ID: 23184 User: 不適用
Process: backgroundTaskHost.exe ID: 23472 User: 不適用
Process: RuntimeBroker.exe ID: 14648 User: 不適用
Process: WidgetService.exe ID: 4132 User: 不適用
Process: vmware-tray.exe ID: 22112 User: 不適用
Process: msedgewebview2.exe ID: 15504 User: 不適用
Process: msedgewebview2.exe ID: 2876 User: 不適用
Process: msedgewebview2.exe ID: 6880 User: 不適用
Process: msedgewebview2.exe ID: 21012 User: 不適用
Process: msedgewebview2.exe ID: 9896 User: 不適用
Process: msedgewebview2.exe ID: 14264 User: 不適用
Process: ShellExperienceHost.exe ID: 16656 User: 不適用
Process: RuntimeBroker.exe ID: 15720 User: 不適用
Process: svchost.exe ID: 16324 User: 不適用
Process: csrss.exe ID: 15120 User: 不適用
Process: winlogon.exe ID: 22660 User: 不適用
Process: fontdrvhost.exe ID: 22644 User: 不適用
Process: dwm.exe ID: 12456 User: 不適用
Process: DSDFunctionKeyCtlService.exe ID: 7996 User: WIN11NB\user1
Process: vmwp.exe ID: 6372 User: 不適用
Process: vmmemMDAG ID: 16244 User: 不適用
Process: sihost.exe ID: 24056 User: WIN11NB\user1
Process: svchost.exe ID: 16052 User: WIN11NB\user1
Process: svchost.exe ID: 12504 User: WIN11NB\user1
Process: igfxEM.exe ID: 9644 User: WIN11NB\user1
Process: svchost.exe ID: 16376 User: WIN11NB\user1
Process: svchost.exe ID: 18652 User: WIN11NB\user1
Process: taskhostw.exe ID: 12240 User: WIN11NB\user1
Process: explorer.exe ID: 17180 User: WIN11NB\user1
Process: SynTPEnh.exe ID: 21536 User: WIN11NB\user1
Process: SynTPHelper.exe ID: 17272 User: WIN11NB\user1
Process: svchost.exe ID: 16812 User: WIN11NB\user1
Process: StartMenuExperienceHost.exe ID: 16592 User: WIN11NB\user1
Process: SearchHost.exe ID: 21176 User: WIN11NB\user1
Process: RuntimeBroker.exe ID: 20824 User: WIN11NB\user1
Process: RuntimeBroker.exe ID: 3792 User: WIN11NB\user1
Process: Widgets.exe ID: 10768 User: WIN11NB\user1
Process: svchost.exe ID: 12184 User: WIN11NB\user1
Process: UserOOBEBroker.exe ID: 9772 User: WIN11NB\user1
Process: dllhost.exe ID: 14920 User: WIN11NB\user1
Process: ctfmon.exe ID: 17044 User: WIN11NB\user1
Process: svchost.exe ID: 17904 User: WIN11NB\user1
Process: hvsimgr.exe ID: 23396 User: WIN11NB\user1
Process: hvsirpcd.exe ID: 17204 User: WIN11NB\user1
Process: hvsirdpclient.exe ID: 2296 User: WIN11NB\user1
Process: RuntimeBroker.exe ID: 11424 User: WIN11NB\user1
Process: WidgetService.exe ID: 12252 User: WIN11NB\user1
Process: TextInputHost.exe ID: 19052 User: WIN11NB\user1
Process: SecurityHealthSystray.exe ID: 9904 User: WIN11NB\user1
Process: OneDrive.exe ID: 23140 User: WIN11NB\user1
Process: picpick.exe ID: 14148 User: WIN11NB\user1
Process: splwow64.exe ID: 2416 User: WIN11NB\user1
Process: svchost.exe ID: 12608 User: WIN11NB\user1
Process: IGCC.exe ID: 18016 User: WIN11NB\user1
Process: RuntimeBroker.exe ID: 17832 User: WIN11NB\user1
Process: ONENOTEM.EXE ID: 4000 User: WIN11NB\user1
Process: vmware-tray.exe ID: 19248 User: WIN11NB\user1
Process: LocationNotificationWindows.exe ID: 20308 User: WIN11NB\user1
Process: RtkAudUService64.exe ID: 15804 User: WIN11NB\user1
Process: msedgewebview2.exe ID: 22176 User: WIN11NB\user1
Process: msedgewebview2.exe ID: 7128 User: WIN11NB\user1
Process: msedgewebview2.exe ID: 24184 User: WIN11NB\user1
Process: msedgewebview2.exe ID: 19492 User: WIN11NB\user1
Process: msedgewebview2.exe ID: 17240 User: WIN11NB\user1
Process: msedgewebview2.exe ID: 18892 User: WIN11NB\user1
Process: svchost.exe ID: 11992 User: WIN11NB\user1
Process: SystemSettings.exe ID: 7240 User: WIN11NB\user1
Process: ApplicationFrameHost.exe ID: 23608 User: WIN11NB\user1
Process: svchost.exe ID: 8112 User: WIN11NB\user1
Process: svchost.exe ID: 23532 User: 不適用
Process: BingSvc.exe ID: 4092 User: WIN11NB\user1
Process: SpotifyWidgetProvider.exe ID: 2676 User: WIN11NB\user1
Process: dllhost.exe ID: 3300 User: WIN11NB\user1
Process: msedge.exe ID: 17864 User: WIN11NB\user1
Process: msedge.exe ID: 10800 User: WIN11NB\user1
Process: msedge.exe ID: 12880 User: WIN11NB\user1
Process: msedge.exe ID: 24412 User: WIN11NB\user1
Process: msedge.exe ID: 3960 User: WIN11NB\user1
Process: msedge.exe ID: 2480 User: WIN11NB\user1
Process: msedge.exe ID: 3768 User: WIN11NB\user1
Process: msedge.exe ID: 14184 User: WIN11NB\user1
Process: msedge.exe ID: 19620 User: WIN11NB\user1
Process: msedge.exe ID: 23812 User: WIN11NB\user1
Process: msedge.exe ID: 23704 User: WIN11NB\user1
Process: Video.UI.exe ID: 17728 User: WIN11NB\user1
Process: RuntimeBroker.exe ID: 17280 User: WIN11NB\user1
Process: ShellExperienceHost.exe ID: 14412 User: WIN11NB\user1
Process: RuntimeBroker.exe ID: 11860 User: WIN11NB\user1
Process: SystemSettingsBroker.exe ID: 25380 User: WIN11NB\user1
Process: PhoneExperienceHost.exe ID: 18216 User: WIN11NB\user1
Process: svchost.exe ID: 19592 User: 不適用
Process: LockApp.exe ID: 14192 User: WIN11NB\user1
Process: RuntimeBroker.exe ID: 26456 User: WIN11NB\user1
Process: chrome.exe ID: 2688 User: WIN11NB\user1
Process: chrome.exe ID: 4652 User: WIN11NB\user1
Process: chrome.exe ID: 14520 User: WIN11NB\user1
Process: chrome.exe ID: 17664 User: WIN11NB\user1
Process: chrome.exe ID: 19284 User: WIN11NB\user1
Process: chrome.exe ID: 24588 User: WIN11NB\user1
Process: chrome.exe ID: 25044 User: WIN11NB\user1
Process: chrome.exe ID: 7548 User: WIN11NB\user1
Process: chrome.exe ID: 5672 User: WIN11NB\user1
Process: chrome.exe ID: 14624 User: WIN11NB\user1
Process: chrome.exe ID: 11800 User: WIN11NB\user1
Process: WmiPrvSE.exe ID: 13016 User: 不適用
Process: dllhost.exe ID: 12532 User: WIN11NB\user1
Process: chrome.exe ID: 14116 User: WIN11NB\user1
Process: chrome.exe ID: 12996 User: WIN11NB\user1
Process: chrome.exe ID: 12704 User: WIN11NB\user1
Process: svchost.exe ID: 18972 User: 不適用
Process: smartscreen.exe ID: 22384 User: WIN11NB\user1
Process: chrome.exe ID: 14600 User: WIN11NB\user1
Process: cmd.exe ID: 22464 User: WIN11NB\user1
Process: conhost.exe ID: 11016 User: WIN11NB\user1
Process: OpenConsole.exe ID: 7260 User: WIN11NB\user1
Process: WindowsTerminal.exe ID: 26600 User: WIN11NB\user1
Process: RuntimeBroker.exe ID: 4700 User: WIN11NB\user1
Process: WmiPrvSE.exe ID: 11184 User: 不適用
Process: cmd.exe ID: 11904 User: WIN11NB\user1
Process: conhost.exe ID: 10660 User: WIN11NB\user1
Process: OpenConsole.exe ID: 23936 User: WIN11NB\user1
Process: Notepad.exe ID: 14588 User: WIN11NB\user1
Process: svchost.exe ID: 17944 User: 不適用
Process: devenv.exe ID: 18784 User: WIN11NB\user1
Process: PerfWatson2.exe ID: 20584 User: WIN11NB\user1
Process: Microsoft.ServiceHub.Controller.exe ID: 24912 User: WIN11NB\user1
Process: ServiceHub.VSDetouredHost.exe ID: 1964 User: WIN11NB\user1
Process: ServiceHub.ThreadedWaitDialog.exe ID: 11732 User: WIN11NB\user1
Process: ServiceHub.RoslynCodeAnalysisService.exe ID: 14796 User: WIN11NB\user1
Process: ServiceHub.SettingsHost.exe ID: 22628 User: WIN11NB\user1
Process: ServiceHub.IdentityHost.exe ID: 19352 User: WIN11NB\user1
Process: ServiceHub.Host.CLR.x86.exe ID: 15744 User: WIN11NB\user1
Process: ServiceHub.DataWarehouseHost.exe ID: 11236 User: WIN11NB\user1
Process: StandardCollector.Service.exe ID: 5680 User: 不適用
Process: ServiceHub.TestWindowStoreHost.exe ID: 20312 User: WIN11NB\user1
Process: ServiceHub.Host.CLR.x64.exe ID: 24632 User: WIN11NB\user1
Process: ServiceHub.Host.CLR.x86.exe ID: 10052 User: WIN11NB\user1
Process: ServiceHub.Host.CLR.x86.exe ID: 11720 User: WIN11NB\user1
Process: svchost.exe ID: 22652 User: 不適用
Process: chrome.exe ID: 26292 User: WIN11NB\user1
Process: MSBuild.exe ID: 13512 User: WIN11NB\user1
Process: conhost.exe ID: 10808 User: WIN11NB\user1
Process: VBCSCompiler.exe ID: 16668 User: WIN11NB\user1
Process: conhost.exe ID: 10280 User: WIN11NB\user1
Process: ScriptedSandbox64.exe ID: 12556 User: WIN11NB\user1
Process: chrome.exe ID: 19332 User: WIN11NB\user1
Process: ConsoleApp1.exe ID: 23644 User: WIN11NB\user1
Process: conhost.exe ID: 11996 User: WIN11NB\user1
Process: OpenConsole.exe ID: 24200 User: WIN11NB\user1
Process: IntelliTrace.exe ID: 12188 User: WIN11NB\user1
Process: conhost.exe ID: 17068 User: WIN11NB\user1
Process: tasklist.exe ID: 26596 User: WIN11NB\user1
Process: conhost.exe ID: 25668 User: WIN11NB\user1
 

(完)

相關

2024年3月23日 星期六

[研究]ASP.NET,WebForm,正則表達式,判斷是否符合「西洋年/月/日」或「西洋年/月」日期格式

[研究]ASP.NET,WebForm,正則表達式,判斷是否符合「西洋年/月/日」或「西洋年/月」日期格式

2024-03-23

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 19

********************************************************************************

C# 正則表達式,判斷是否符合YYYY/MM/DD,其中 YYYY 是 4位數西洋年, MM 是2位數月份,D是2位數日期

Default2.aspx.cs

<%@ Page Language="C#" AutoEventWireup="true" 
    CodeBehind="Default.aspx.cs" Inherits="WebApplication2.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Label ID="Label1" runat="server" Text="符合者:"></asp:Label>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Text.RegularExpressions;

namespace WebApplication2
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string[] dates = {
             "2024/03/19",
             "2024/3/19",
             "2024/03/9",
             "2024/3/9",
             "2024/03",
             "2024/3",
             "2024/03/0",
             "2024/0",
             "2024/0/19",
             "2024/03/0"
            };
            //是否符合YYYY/MM/DD 
            string pattern = @"^\d{4}/(0[1-9]|1[0-2])/(0[1-9]|[1-2][0-9]|3[0 -1])$";

            foreach (string date in dates)
            {
                //Console.WriteLine($"{date}: {Regex.IsMatch(date, pattern)}");
                if (Regex.IsMatch(date, pattern))
                    Label1.Text = Label1.Text + date + "<br />";
            }
        }
        //---
    }
}

********************************************************************************

C# 正則表達式,判斷是否符合

YYYY/MM/DD 或 YYYY/M/D 或 YYYY/MM/D 或 YYYY/M/DD 或 YYYY/MM 或 YYYY/M 或 YYYY/0M/0D 或 YYYY/MM/0D 或 YYYY/0M/DD,其中 YYYY 是 4位數西洋年,M 或 MM 是月份,D 或 D 是日期

Default4.aspx.cs

using System;
using System.Text.RegularExpressions;

namespace WebApplication1
{
    public partial class Default4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string[] dates = {
             "2024/03/19",
             "2024/3/19",
             "2024/03/9",
             "2024/3/9",
             "2024/03",
             "2024/3",
             "2024/03/0",
             "2024/03/19",
             "2024/03/0",
             "2024/03/19",
             "2024/0",
             "2024/03/19",
             "2024/0/19",
             "2024/03/19",
             "2024/0/0",
             "2024/03/0"
         };

            string pattern = @"^(\d{4})/(0?[1-9]|1[0-2])/(0?[1-9]|1\d|2[0-9]|3[01])$";

            foreach (string date in dates)
            {
                //Console.WriteLine($"{date}: {Regex.IsMatch(date, pattern)}");
                if (Regex.IsMatch(date, pattern))
                    Label1.Text = Label1.Text + date + "<br />";
            }
        }
    }
}

這個範例程式碼使用了正規表示式^(\d{4})/(0?[1-9]|1[0-2])/(0?[1-9]|1\d|2[0 -9]|3[01])$ 來符合日期字串。 其中:

  • ^ 表示字串的開始。
  • \d{4} 以四位數字表示表示的年份。
  • / 匹配斜線字元。
  • (0?[1-9]|1[0-2]) 配對月份,可以是1位或2位數字,1-12之間。
  • (0?[1-9]|1\d|2[0-9]|3[01]) 匹配日期,可以是1位或2位數字,根據月份不同,允許的範圍為1-31之 間。
  • $ 表示字串的結尾。

實際測試

********************************************************************************

C# 正則表達式,判斷是否符合

YYYY/MM/DD 或 YYYY/M/D 或 YYYY/MM/D 或 YYYY/M/DD 或 YYYY/MM 或 YYYY/M 或 YYYY/0M/0D 或 YYYY/MM/0D 或 YYYY/0M/DD 或 YYYY/MM 或 YYYY/0M,

其中 YYYY 是 4位數西洋年,M 或 MM 是月份,D 或 D 是日期

string pattern = @"^\d{4}/(0?[1-9]|1[0-2])(/(0?[1-9]|[12]\d|3[01]))?$";

********************************************************************************

提取日期部分並顯示。

SELECT CAST(YourDateTimeColumn AS DATE) AS DatePart FROM YourTable;

SELECT CONVERT(DATE, YourDateTimeColumn) AS DatePart FROM YourTable;

(完)

相關

2024年3月19日 星期二

[研究]C#,顯示「帳戶原則」(「密碼原則」、「帳戶鎖定原則」)結果

[研究]C#,顯示「帳戶原則」(「密碼原則」、「帳戶鎖定原則」)結果

2024-03-19

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 19

密碼原則

  • 使用可還原的加密來存放密碼
  • 密碼必須符合複雜性需求
  • 密碼最短使用期限
  • 密碼最長使用期限
  • 強制執行密碼歷程記錄
  • 最小密碼長度

已啟用 ? 已停用?  尚未定義 ?


********************************************************************************

WinForm 程式

using System;
using System.Windows.Forms;

namespace ServerHealthCheckWindowsFormsApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                richTextBox1.AppendText("rsop.msc\n");
                richTextBox1.AppendText("電腦設定\\Windows設定\\安全性設定\\帳戶原則\\密碼原則\n\n");

                #region === 使用可還原的加密來存放密碼 ===
                string registryPath = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System";
                object clearTextPasswordValue = Microsoft.Win32.Registry.GetValue(registryPath, "ClearTextPassword", null);
                if (clearTextPasswordValue != null)
                {
                    int value = Convert.ToInt32(clearTextPasswordValue);
                    if (value == 1)
                    {
                        richTextBox1.AppendText("使用可還原的加密來存放密碼:已啟用\n");
                    }
                    else
                    {
                        richTextBox1.AppendText("使用可還原的加密來存放密碼:已停用\n");
                    }
                    //精簡寫法
                    //richTextBox1.AppendText("使用可還原的加密來存放密碼:" + ((Boolean)clearTextPasswordValue ? "已啟用" : "已停用") + "\n");
                }
                else
                {
                    
                    richTextBox1.AppendText("使用可還原的加密來存放密碼:尚未定義\n");
                }
//精簡寫法 (尚未測試)
richTextBox1.AppendText($"使用可還原的加密來存放密碼:{(clearTextPasswordValue != null && (int)clearTextPasswordValue == 1 ? "
已啟用 " : (clearTextPasswordValue != null && (int)clearTextPasswordValue != 1 ? " 已停用 " : " 尚未定義 "))}\n"); #endregion === 使用可還原的加密來存放密碼 === #region === 密碼必須符合複雜性需求 === registryPath = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"; // Read the value of the "PasswordComplexity" registry key object passwordComplexityValue = Microsoft.Win32.Registry.GetValue(registryPath, "PasswordComplexity", null); if (passwordComplexityValue != null) { richTextBox1.AppendText("密碼必須符合複雜性需求:" + ((Boolean)passwordComplexityValue ? "已啟用" : "已停用") + "\n"); } else { richTextBox1.AppendText("密碼必須符合複雜性需求:尚未定義\n"); } #endregion === 密碼必須符合複雜性需求 === #region === 密碼最短使用期限 === registryPath = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"; object minimumPasswordAgeValue = Microsoft.Win32.Registry.GetValue(registryPath, "MinimumPasswordAge", null); if (minimumPasswordAgeValue != null) { int valueInSeconds = Convert.ToInt32(minimumPasswordAgeValue); int valueInDays = valueInSeconds / (60 * 60 * 24); // Convert to number of days richTextBox1.AppendText("密碼最短使用期限:" + valueInDays.ToString() + " 天\n"); } else { richTextBox1.AppendText("密碼最短使用期限:尚未定義\n"); } #endregion === 密碼最短使用期限 === #region === 密碼最長使用期限=== registryPath = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"; object maximumPasswordAgeValue = Microsoft.Win32.Registry.GetValue(registryPath, "MaximumPasswordAge", null); if (maximumPasswordAgeValue != null) { int valueInSeconds = Convert.ToInt32(maximumPasswordAgeValue); int valueInDays = valueInSeconds / (60 * 60 * 24); // Convert to number of days richTextBox1.AppendText("密碼最長使用期限:" + valueInDays.ToString() + " 天\n"); } else { richTextBox1.AppendText("密碼最長使用期限:尚未定義\n"); } #endregion === 密碼最長使用期限=== #region === 密碼最短使用期限、密碼最長使用期限=== // Console 寫法 //object minimumPasswordAgeValue = Microsoft.Win32.Registry.GetValue(registryPath, "MinimumPasswordAge", null); //object maximumPasswordAgeValue = Microsoft.Win32.Registry.GetValue(registryPath, "MaximumPasswordAge", null); string minimumPasswordAgeStatus = minimumPasswordAgeValue != null ? "已設定" : "尚未設定"; string minimumPasswordAgeValueText = minimumPasswordAgeValue != null ? $"(設定值:{minimumPasswordAgeValue})" : ""; string maximumPasswordAgeStatus = maximumPasswordAgeValue != null ? "已設定" : "尚未設定"; string maximumPasswordAgeValueText = maximumPasswordAgeValue != null ? $"(設定值:{maximumPasswordAgeValue})" : ""; Console.WriteLine("密碼最短使用期限:{0} {1}", minimumPasswordAgeStatus, minimumPasswordAgeValueText); Console.WriteLine("密碼最長使用期限:{0} {1}", maximumPasswordAgeStatus, maximumPasswordAgeValueText); #endregion === 密碼最短使用期限、密碼最長使用期限=== #region === 強制執行密碼歷程記錄 === registryPath = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"; object passwordHistorySizeValue = Microsoft.Win32.Registry.GetValue(registryPath, "PasswordHistorySize", null); if (passwordHistorySizeValue != null) { int value = Convert.ToInt32(passwordHistorySizeValue); if (value > 0) { richTextBox1.AppendText("強制執行密碼歷程記錄:已停用\n"); richTextBox1.AppendText("密碼歷程記錄秒數:" + value.ToString() + "\n"); } else { richTextBox1.AppendText("強制執行密碼歷程記錄:已停用\n"); } } else { richTextBox1.AppendText("強制執行密碼歷程記錄:尚未定義\n"); } #endregion === 強制執行密碼歷程記錄 === #region === 最小密碼長度 === registryPath = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"; object minimumPasswordLengthValue = Microsoft.Win32.Registry.GetValue(registryPath, "MinimumPasswordLength", null); if (minimumPasswordLengthValue != null) { int value = Convert.ToInt32(minimumPasswordLengthValue); Console.WriteLine("Minimum password length: {0}", value); richTextBox1.AppendText("最小密碼長度:" + value.ToString() + "\n"); } else { richTextBox1.AppendText("最小密碼長度:尚未定義\n"); } #endregion === 最小密碼長度 === } catch (Exception ex) { richTextBox1.AppendText("發生異常:" + ex.Message.ToString()); } // Console 增加 //finally //{ // Console.WriteLine("按下任意鍵退出..."); // Console.ReadKey(); //} } } }

********************************************************************************

【電腦設定\Windows設定\安全性設定\帳戶原則\帳戶鎖定原則】


重設帳戶鎖定計數器的時間間隔

帳戶鎖定期間

帳戶鎖定閾值

已啟用 ? 已停用? 尚未設定 ? 值 ? 繁體中文

using Microsoft.Win32;
using System;
using System.Windows.Forms;

namespace AccountLockoutPolicyExample
{
     public partial class MainForm : Form
     {
         public MainForm()
         {
             InitializeComponent();
         }

         private void MainForm_Load(object sender, EventArgs e)
         {
             try
             {
                 // 本機的登錄路徑
                 string registryPath = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon";

                 // 讀取 "ResetLockoutCount" 註冊表鍵的值
                 object resetLockoutCountValue = Registry.GetValue($"HKEY_LOCAL_MACHINE\\{registryPath}", "ResetLockoutCount", null);

                 // 檢查策略的狀態
                 string resetLockoutCountStatus = GetPolicyStatus(resetLockoutCountValue);

                 // 讀取 "LockoutDuration" 註冊表鍵的值
                 object lockoutDurationValue = Registry.GetValue($"HKEY_LOCAL_MACHINE\\{registryPath}", "LockoutDuration", null);
                 string lockoutDurationStatus = GetPolicyStatus(lockoutDurationValue);

                 // 讀取 "LockoutThreshold" 註冊表鍵的值
                 object lockoutThresholdValue = Registry.GetValue($"HKEY_LOCAL_MACHINE\\{registryPath}", "LockoutThreshold", null);
                 string lockoutThresholdStatus = GetPolicyStatus(lockoutThresholdValue);

                 // 顯示結果
                 richTextBox.Text = $"重設帳戶鎖定計數器的時間間隔:{resetLockoutCountStatus}\n" +
                                     $"帳戶鎖定期間:{lockoutDurationStatus}\n" +
                                     $"帳戶鎖定閾值:{lockoutThresholdStatus}";
             }
             catch (Exception ex)
             {
                 richTextBox.Text = $"發生例外狀況:{ex.Message}";
             }
         }

         private string GetPolicyStatus(object policyValue)
         {
             if (policyValue != null)
             {
                 return $"已啟用 (值:{policyValue})";
             }
             else
             {
                 return "尚未設定";
             }
         }
     }
}


(完)

相關

2023年3月13日 星期一

[研究][ASP.NET]DropDownList預先選今年

[研究][ASP.NET]DropDownList預先選今年

2023-03-13

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#

********************************************************************************

Default.aspx

年:<asp:DropDownList ID="DropDownList_Year" runat="server" AutoPostBack="true">
        <asp:ListItem>2024</asp:ListItem>
        <asp:ListItem>2023</asp:ListItem>
        <asp:ListItem>2022</asp:ListItem>
        <asp:ListItem>2021</asp:ListItem>
    </asp:DropDownList>

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // 如果是在 DetailsView1 中,找出 DropDownList 控制項
        //DropDownList ddlYear = (DropDownList)DetailsView1.FindControl("DropDownList_Year"); 
        
		// 設定預選值為今年的年份
        int currentYear = DateTime.Now.Year-1;
        if (currentYear > 2024)
        {
            currentYear = 2024;
        }
        DropDownList_Year.SelectedValue = currentYear.ToString();
    }
}

(完)


2022年5月16日 星期一

[研究][ASP.NET][C#]判斷星期幾

[研究][ASP.NET][C#]判斷星期幾

2022-05-14

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#

參考

作法:從特定日期擷取一星期的哪一日 | Microsoft Docs
https://docs.microsoft.com/zh-tw/dotnet/standard/base-types/how-to-extract-the-day-of-the-week-from-a-specific-date

DateTime.DayOfWeek 屬性 (System) | Microsoft Docs
https://docs.microsoft.com/zh-tw/dotnet/api/system.datetime.dayofweek?view=netframework-4.8

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" 
    CodeBehind="Default.aspx.cs" Inherits="WebApplication2.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
        <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label><br />
        <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label><br />
        <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label><br />
        <asp:Label ID="Label6" runat="server" Text="Label"></asp:Label><br />
        <asp:Label ID="Label7" runat="server" Text="Label"></asp:Label><br />
        <asp:Label ID="Label99" runat="server" Text="Label"></asp:Label><br />   
    </form>
</body>
</html>


Default.aspx.cs

using System;

namespace WebApplication2
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = DateTime.Now.DayOfWeek.ToString();    // Monday
            Label2.Text = DateTime.Now.DayOfWeek.ToString("d"); // 1

            DateTime dateValue = new DateTime(2022, 05, 14);
            Label3.Text = dateValue.DayOfWeek.ToString("d");    //6

            DateTime dateValue2 = new DateTime(2022,05,15);
            Label4.Text = dateValue2.DayOfWeek.ToString("d");    //0

            Label5.Text = System.Globalization.CultureInfo.CurrentCulture.
                DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek);    //星期一
            Label6.Text = DateTime.Now.ToString("dddd", 
                new System.Globalization.CultureInfo("zh-tw")); //星期一

            string dd=new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", }
                [Convert.ToInt16(DateTime.Now.DayOfWeek.ToString("D"))];
            Label7.Text = dd;   //星期一

            string dt = DateTime.Today.DayOfWeek.ToString();
            string week = "";
            switch (dt)
            {
                case "Monday":
                    week = "星期一";
                    break;
                case "Tuesday":
                    week = "星期二";
                    break;
                case "Wednesday":
                    week = "星期三";
                    break;
                case "Thursday":
                    week = "星期四";
                    break;
                case "Friday":
                    week = "星期五";
                    break;
                case "Saturday":
                    week = "星期六";
                    break;
                case "Sunday":
                    week = "星期日";
                    break;
            }
            Label99.Text = week;
        }
    }
}

結果

(完)


2018年7月10日 星期二

[研究]ASP.NET WebForm 存取 Oracle Database

[研究]ASP.NET WebForm 存取 Oracle Database

2018-07-10

這是敝人第一次試用。
環境:Visual Studio 2017 v15.7.4
Oracle Database 12c Standard 是另外一台電腦

連上官方網站,下載 ORAC for Visual Studio 2017

Oracle Data Access Components - .NET Downloads
http://www.oracle.com/technetwork/topics/dotnet/downloads/net-downloads-160392.html


(下圖) 下載

(下圖) 下載得到 ODTforVS2017_122011.exe
注意網頁上的說明,例如 Visual Studio 2017 至少要 v15.5.4 版






(下圖)
C:\Program Files (x86)\Oracle Developer Tools for VS2017\asp.net\SQL
因為敝人這台電腦有 Visual Studio 2017,但沒有 Oracle Database (在另外一台)
所以這一堆 .sql 敝人沒有執行

(下圖) 只是看一下是甚麼

(下圖) 這裡有範例
C:\Program Files (x86)\Oracle Developer Tools for VS2017\samples\odp.net.managed\4\

敝人複製一份 DataReader 目錄,拿來修改和測試

(下圖) 修改和測試



// C#

using System;
using System.Data;
using System.Data.Common;
using Oracle.ManagedDataAccess.Client;

class VisibleFieldCountSample
{
  static void Main(string[] args)
  {
        // 依照自己環境修改
        string constr = "User Id=scott; Password=tiger; Data Source=oracle;";

        using (OracleConnection conn = new OracleConnection(constr))
    {
      conn.ConnectionString = constr;
      try
      {
        conn.Open();
        OracleCommand cmd = new OracleCommand();
        cmd.Connection = (OracleConnection)conn;

        //to gain access to ROWIDs of the table
        cmd.AddRowid = true;
        //cmd.CommandText = "select empno, ename from emp;";
        cmd.CommandText = "select llevel from A;";

        OracleDataReader reader = cmd.ExecuteReader();
     
        int visFC = reader.VisibleFieldCount; //Results in 2
        int hidFC = reader.HiddenFieldCount;  // Results in 1

        Console.WriteLine("Visible field count: " + visFC);
        Console.WriteLine("Hidden field count: " + hidFC);

        reader.Dispose();
        cmd.Dispose();
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.Message);
        Console.WriteLine(ex.StackTrace);
      }
    }
  }
}



ORA-12504: TNS:未提供監聽器 CONNECT_DATA 中的 SERVICE_NAME

(下圖) 查了一下,要修改組態檔案
C:\Program Files (x86)\Oracle Developer Tools for VS2017\network\admin\tnsnames.ora


(下圖) 新增一筆組態,請依照自己環境修改
Data Source 的值是 (資料來源別名)?

(資料來源別名) =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 資料庫主機IP或網址 )(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = 服務名稱)
    )
  )


(下圖) 只是看一下,沒有修改

(下圖) 執行成功


********************************************************************************

如果是自己另另外建立的方案






********************************************************************************

如果要在 Web.Config 中加入 Oracle Database 連線字串

<add name="oracleConnectionString" connectionString="User Id=帳號; Password=密碼; Data Source=來源IP;Persist Security Info=True;" providerName="System.Data.OracleClient" />

(完)

相關

Oracle Data Access Components - .NET Downloads
http://www.oracle.com/technetwork/topics/dotnet/downloads/net-downloads-160392.html

Data Provider for .NET Developer's Guide
https://docs.oracle.com/database/121/ODPNT/client.htm#ODPNT0008

官方範例
https://docs.oracle.com/database/121/ODPNT/OracleConnectionClass.htm#ODPNT758

Oracle SQL Developer
https://www.oracle.com/database/technologies/appdev/sql-developer.html

System.Data.OracleClient Namespace
https://msdn.microsoft.com/en-us/library/system.data.oracleclient(v=vs.110).aspx

.Net 開發者與 Oracle Database (六)
https://dotblogs.com.tw/chhuang/2012/06/17/72862

Oracle .NET 开发人员中心
2016 年 10 月 6 日更新
http://www.oracle.com/technetwork/cn/topics/dotnet/whatsnew/index.html

Oracle .NET Developer Center
https://www.oracle.com/database/technologies/appdev/dotnet.html

Oracle's Commitment to .NET
http://images.slideplayer.com/19/5843029/slides/slide_5.jpg



Fundamentals of Database Chapter 7 Database Technologies.
https://slideplayer.com/slide/7457134/

Getting Started with Oracle and.NET Christian Shay Principal Product Manager,.NET and Windows Oracle USA.
https://slideplayer.com/slide/5843029/

C# - Oracle - How To Connect C# with Oracle Using ODAC - ODP.NET
https://www.youtube.com/watch?v=cP6UatAZ76s