c# string concatenations

การต่อ String ใน C# ทำได้  4 แบบดังนี้ ใช้เครื่องหมาย plus (+) แบบยอดนิยมใช้งานกันบ่อยๆ. string txt = “aaa”+”bbb”+”ccc”; ใช้ string.Concat() สะดวกับการต่อ list หรือ array มาก. string [] s = { “ManU”, “Liverpool”, “Asenal” }; Console.WriteLine(string.Concat(s)); ใช้ string.Format() เหมาะสำหรับต่อ string และจัดการรูปแบบการแสดงผลด้วยไปในคราวเดียวกันเลย. string value1 = “Hello World!”; int value2 = 2557; DateTime value3 = DateTime.Now(); string result = string.Format(“{0}: {1:0.0} – {2:dd-mm-yyyy}”, value1, value2, value3); ใช้ stringBuilder เหมาะสำหรับการต่อ sting ที่มีปริมาณมากๆจะทำงานได้เร็วประมาณ 1000 ขึ้นไป. StringBuilder returnNumber = new StringBuilder(1500); for(int i = 0; i<1500; i++) { returnNumber.Append(i.ToString()); } ถ้ามีเวลาจะมาขยายเรื่องนี้ในหัวข้อการทดสอบ Performance จะมาลองดูว่าแบบไหนเร็วกว่ากันไปซักเท่าไร ตอนนี้เอาเท่านี้ไปก่อนแล้วกันครับ

Read More »

วิธีการเขียน Script ตรวจสอบ Server Performance สำหรับเครื่อง Windows 2008 R2

ขอนำเสนอ Powershell Script สำหรับใช้ Monitor Server Performance สำหรับเครื่อง Windows 2008 R2 ขึ้นไปดังนี้ การติดตั้ง PowerShell Editor และ วิธีใช้งานบน Windows 2008 1. ติดตั้งโดยไปที่ Server Manager -> Features -> Add Features 2. เลือกหัวข้อ Windows PowerShell Integrated Script Environment (ISE) 3. ให้เลือกใช้งาน Version X86 ซึ่งเมื่อลงแล้ว Icon จะอยู่ที่ Accessories -> Windows PowerShell -> Windows PowerShell ISE (x86) คำสั่งในการเปิด Execution Policy Set-ExecutionPolicyUnrestricted -Force การสร้าง Loop เพื่อให้สามารถรันได้ตลอดเวลา While($true){ …..[Monitor Script]….. } คำสั่งตรวจหา IP ของ Server Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object { $_.Description -eq “Intel(R) Dual Band Wireless-N 7260” } | ForEach-Object { $ipaddr = $_.IPAddress } $ip = $ipaddr[0] คำสั่งตรวจสอบ Max CPU Clock Speed Get-WmiObject-class win32_processor -Property  “maxclockspeed” | ForEach-Object {$clockspeed = $_.maxclockspeed } Get-WmiObject-class win32_computersystem -Property “NumberOfLogicalProcessors” | ForEach-Object { $lprocessor = $_.NumberOfLogicalProcessors } $sumclockspeed = $clockspeed*$lprocessor คำสั่งตรวจสอบ Current CPU (Get-Counter “\Processor(_Total)\% Processor Time”).CounterSamples | ForEach-Object {$cpuuse = $_.CookedValue}     $cpuuse = [math]::round($cpuuse,0)     $clockspeed = ($cpuuse*$sumclockspeed)/100     $clockspeed = [math]::round($clockspeed,0)     $pcpuuse = $clockspeed/$sumclockspeed     $pcpuuse = [math]::round($pcpuuse,0) คำสั่งตรวจสอบ Maximum Memory $sumram = 0 Get-WMIObject-class win32_physicalmemory -Property “Capacity”  | ForEach-Object { $sumram = $sumram + $_.Capacity} $sumram = $sumram/(1024*1024) คำสั่งตรวจสอบ Current Memory (Get-Counter “\Memory\Available MBytes”).CounterSamples | ForEach-Object {$memfree = $_.CookedValue}     $memuse = $sumram-$memfree;     $pmemuse = ($memuse/$sumram)*100     $pmemuse = [math]::round($pmemuse,0) คำสั่งตรวจสอบ Connection  $netstat = “netstat

Read More »

แนะนำ ffDiaporama โปรแกรมนำภาพถ่ายมาทำภาพยนตร์ (Movie creator)

แนะนำ ffDiaporama โปรแกรมนำภาพถ่ายมาทำภาพยนตร์ (Movie creator) Movie creator from photos and video clips with ffDiaporama ตัวอย่าง http://ftp.psu.ac.th/pub/cc/2557cop1/ccadmintour2014.mp4 (392M) โปรแกรม ffDiaporama เป็นโปรแกรมทำภาพยนตร์จากภาพถ่ายและคลิปวิดีโอ มีทั้งเวอร์ชั่นที่ใช้งานบน linux และ Windows มีเว็บไซต์อยู่ที่นี่ URL: http://ffdiaporama.tuxfamily.org/ โดยทั่วไปวิธีติดตั้งโปรแกรมเพิ่มใน linux mint คือ เลือก Menu > Software Manager แต่โปรแกรม ffDiaporama นั้นมันเป็นเวอร์ชั่น 1.5 ซึ่งยังขาดความสามารถในการแปลง (render) จัดได้ว่ามันไม่ทันสมัย (outdated!) จึงจำเป็นต้องติดตั้งด้วยวิธี command line จากเว็บไซต์ของผู้พัฒนาโปรแกรมโดยตรง (Launchpad on the ffDiaporama-stable PPA) ทำคำสั่งนี้เพื่อเพิ่มชื่อ server ต้นทางที่เก็บโปรแกรม sudo add-apt-repository ppa:ffdiaporamateam/stable จะได้ผลลัพธ์แบบนี้ mint@mint-PSU ~ $ sudo add-apt-repository ppa:ffdiaporamateam/stable You are about to add the following PPA to your system: This PPA supplies stable packages of ffDiaporama 2.1 For more information on ffDiaporama see : http://ffdiaporama.tuxfamily.org More info: https://launchpad.net/~ffdiaporamateam/+archive/stable Press [ENTER] to continue or ctrl-c to cancel adding it เมื่อกด Enter แล้วจะได้แบบนี้ Executing: gpg –ignore-time-conflict –no-options –no-default-keyring –homedir /tmp/tmp.JiNHX5a3T2 –no-auto-check-trustdb –trust-model always –keyring /etc/apt/trusted.gpg –primary-keyring /etc/apt/trusted.gpg –keyserver hkp://keyserver.ubuntu.com:80 –recv-keys 77296259 gpg: requesting key 77296259 from hkp server keyserver.ubuntu.com gpg: key 77296259: public key “Launchpad PPA for ffDiaporama Team” imported gpg: Total number processed: 1 gpg:               imported: 1  (RSA: 1) สั่งอัปเดตรายการซอฟต์แวร์ sudo apt-get update ติดตั้งโปรแกรม sudo apt-get install ffdiaporama จะได้ผลลัพธ์แบบนี้ mint@mint-PSU ~ $ sudo apt-get install ffdiaporama Reading package lists… Done Building dependency tree Reading state information… Done The following packages were automatically installed and are no longer required: ffdiaporama-data libqimageblitz4 Use ‘apt-get autoremove’

Read More »

วิธีตรวจสอบเว็บไซต์ที่โดน Hack #15

จาก วิธีตรวจสอบเว็บไซต์ที่โดน Hack #7 : การตรวจสอบ Windows Server ที่ถูก Hack ด้วย PowerShell ซึ่งเป็นกระบวนการตรวจสอบ Windows Server ซึ่งโดนเจาะด้วยช่องโหว่ JCE ของ Joomla ก็มีคนถามว่า จะมีกระบวนการแก้ไขป้องกัน เหมือนกับที่ทำกับ Apache ซึ่งแสดงใน วิธีตรวจสอบเว็บไซต์ที่โดน Hack #12 : เทคนิคการตั้งค่า Apache Web Server เพื่อให้ปลอดภัยจากช่องโหว่ ตอนนี้มีคำตอบแล้วครับ เทคนิคนี้ ใช้ผ่าน Internet Information Services (IIS) Manager โดยการแก้ไข Request Filtering ในระดับ Web Server เลย โดยดำเนินการตามวิธีการต่อไปนี้ เรียก Command ด้วย การกดปุ่ม Windows + R แล้ว พิมพ์ inetmgr แล้วกดปุ่ม Enter คลิกเว็บเซิร์ฟเวอร์ของเครื่องที่ต้องการใน Connection Tab (ตัวอย่างในภาพ คลิกที่ WUNCAWEBSEC) ต่อไป ภายใต้หัวข้อ IIS ให้ Double-Click ที่ Request Filtering คลิกที่ Rules tab เพิ่มกฏสำหรับ JCE Bot ซึ่ง ไม่ต้องการให้ PHP ทำงานภายใต้ URL ซึ่งมีข้อความว่า “images/stories” โดย ไปที่ Action ด้านขวามือ แล้ว คลิกที่ Add Filtering Rules … แล้วใส่ข้อมูลตามภาพ แล้วคลิกปุ่ม OK เพิ่มกฏสำหรับ Upload โฟลเดอร์ ซึ่ง ไม่ต้องการให้ PHP ทำงานภายใต้ URL ซึ่งมีข้อความว่า “upload” โดย ไปที่ Action ด้านขวามือ แล้ว คลิกที่ Add Filtering Rules … แล้วใส่ข้อมูลตามภาพ แล้วคลิกปุ่ม OK ผลที่ได้ใน Rules tab ทดสอบผลการทำงาน สมมุติเดิมโดนวางไฟล์ Backdoor ไว้ที่ http://localhost/corin/images/stories/backdoor.php แต่เมื่อตั้ง Rules ดังกล่าวแล้ว จะทำให้ Hacker ไม่สามารถเรียกใช้งาน PHP ที่วางไว้ใน images/stories ได้ โดยจะได้ Error เช่นนี้ วิธีนี้มีข้อดีคือ สามารถป้องกันการใช้งาน PHP ใน images/stories (และใน upload โฟลเดอร์) แต่ยังสามารถเรียกไฟล์ภาพและไฟล์อื่นๆได้ตามปรกติ เช่น http://localhost/corin/images/stories/clownspin.gif ลองใช้งานดูครับ 😉

Read More »

How to: PSU OpenVPN Ubuntu 14.04/15.04/15.10/Linux Mint 17/17.1/17.2

ติดตั้ง net-workmanager-openvpn คลิก Ubuntu Button แล้วพิมพ์ netw เลือก Network Connections ได้ดังภาพ คลิก Add ได้ดังภาพ เลือก OpenVPN แล้วคลิก Create… กรอกการตั้งค่าต่างๆ ดังภาพ โดยสามารถโหลด PSUCAcert.cer ได้จาก http://netserv.cc.psu.ac.th/documents/doc_download/269-vpnpackageforallwin7 http://ftp.psu.ac.th/pub/openvpn/openvpncer.zip โหลดมาแล้วให้คลายซิบแล้วแยกเอาไฟล์ PSUCAcert.cer ออกมา กรอก User name และ Password ของตัวเอง คลิก Advanced เลือกดัังภาพ กด OK ออกมาแล้วคลิกที่ IPv4 Settings ตั้งค่าตามภาพ คลิก IPv6 Settings ตั้งค่าตามภาพ กลับมาแท็บ VPN คลิก Save… ได้ดังภาพ คลิก Close เหลือบมองมุมขวาบนจะมีรูปภาพ หรือ ให้คลิกที่รูปนี้แล้วเลือก VPN Connection -> PSU รอจนมีข้อความดังภาพปรากฎที่มุมบนขวา เป็นอันเสร็จ สำหรับ Linux Mint 17 ติดตั้ง network-manager-openvpn-gnome แทน นอกนั้นเหมือนกัน เพิ่มเติม เพื่อให้สามารถคลายแฟ้ม vpn_package_for_all_xp-vista-7.exe ได้ต้องติดตั้งโปแกรมเหล่านี้เพิ่มเติม p7zip-rar, p7zip-full, unace, unrar, zip, unzip, sharutils, rar, uudeview mpack, arj, cabextract, file-roller โดยพิมพ์คำสั่ง sudo apt-get install p7zip-rar p7zip-full unace unrar zip unzip sharutils rar uudeview mpack arj cabextract file-roller จบจริงๆ … ขอให้สนุกครับ

Read More »