Tag: csharp

C#

Get MAC Address with C#

public string GetLocalIPAddress() { foreach (NetworkInterface NetI in NetworkInterface.GetAllNetworkInterfaces()) { if (NetI.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || NetI.NetworkInterfaceType == NetworkInterfaceType.Ethernet && NetI.OperationalStatus == OperationalStatus.Up) { foreach (UnicastIPAddressInformation UniIpAddrInfo in NetI.GetIPProperties().UnicastAddresses) { if (UniIpAddrInfo.Address.AddressFamily != AddressFamily.InterNetwork || UniIpAddrInfo.AddressPreferredLifetime == UInt32.MaxValue) continue;   return UniIpAddrInfo.Address.ToString(); } } }   return "Unknown"; }   public string GetMacAddress() { foreach (NetworkInterface …

C#

Get local IP address with C#

public string GetLocalIPAddress() { foreach (NetworkInterface NetI in NetworkInterface.GetAllNetworkInterfaces()) { if (NetI.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || NetI.NetworkInterfaceType == NetworkInterfaceType.Ethernet && NetI.OperationalStatus == OperationalStatus.Up) { foreach (UnicastIPAddressInformation UniIpAddrInfo in NetI.GetIPProperties().UnicastAddresses) { if (UniIpAddrInfo.Address.AddressFamily != AddressFamily.InterNetwork || UniIpAddrInfo.AddressPreferredLifetime == UInt32.MaxValue) continue;   return UniIpAddrInfo.Address.ToString(); } } }   return "Unknown"; }public string GetLocalIPAddress() { foreach (NetworkInterface NetI in …

C#

How to get bios identifier with C#

public string GetBiosId() { try { string bios = ""; using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_BIOS")) { foreach (ManagementObject mObject in searcher.Get()) { bios = mObject["Manufacturer"].ToString(); break; } } return (!string.IsNullOrEmpty(bios)) ? bios : "Unknown"; } catch { } return "Unknown"; }public string GetBiosId() { try { string bios = ""; using …

C#

Explorer theme for ListView control

[DllImport("uxtheme.dll", CharSet = CharSet.Unicode)] public extern static int SetWindowTheme(IntPtr hWnd, string pszSubAppName, string pszSubIdList);   //overwrite control’s OnHandleCreated method protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); if (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major >= 6) { SetWindowTheme(this.Handle, "explorer", null); } }[DllImport("uxtheme.dll", CharSet = CharSet.Unicode)] public extern static int SetWindowTheme(IntPtr hWnd, string pszSubAppName, string pszSubIdList); //overwrite control’s …

C#

Compute hash values for files in c#

MD5: using (MD5 md5 = MD5.Create()) { using (FileStream stream = File.OpenRead(filename)) { return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower(); } }using (MD5 md5 = MD5.Create()) { using (FileStream stream = File.OpenRead(filename)) { return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower(); } } SHA1: using (SHA1 sha1 = SHA1.Create()) { using (FileStream stream = File.OpenRead(filename)) { return BitConverter.ToString(sha1.ComputeHash(stream)).Replace("-", "").ToLower(); } }using (SHA1 sha1 = …

C#

Get Processes and Windows using C#

Add a reference in your project to System.Management namespace. Windows API: private delegate bool EnumWindowsProc(IntPtr hWnd, int lParam); [DllImport("user32.dll")] static extern int GetWindowTextLength(IntPtr hWnd); [DllImport("user32.dll")] private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); [DllImport("user32.dll")] private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, int lParam);private delegate bool EnumWindowsProc(IntPtr hWnd, int lParam); [DllImport("user32.dll")] static extern int GetWindowTextLength(IntPtr …

C#

Get Windows version, Architecture and Service pack

Let’s see how can we get Windows Version, CPU architecture and Windows Service Pack in Windows using C#. We are going to use GetVersionEx and OSVERSIONINFOEX structure from Windows API. Windows API: [StructLayout(LayoutKind.Sequential)] private struct OSVERSIONINFOEX { public int dwOSVersionInfoSize; public int dwMajorVersion; public int dwMinorVersion; public int dwBuildNumber; public int dwPlatformId; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = …

C#, PHP

Encrypting communication between C# and PHP with Rijndael 256

PHP Encryption $iv_to_pass_to_decryption = ”; function encrypt($text, $key) { $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), MCRYPT_RAND); $iv_to_pass_to_decryption = base64_encode($iv); return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv); }$iv_to_pass_to_decryption = ”; function encrypt($text, $key) { $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), MCRYPT_RAND); $iv_to_pass_to_decryption = base64_encode($iv); return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv); } Decryption function decrypt($text, $key, $iv) { $text = base64_decode($text); $iv …

C#

Send message to Skype contact using C#

Let’s see how can we send a message in Skype from a C# application. I chose skype just for the demonstration. You could pick any application and do anything with the following code. SendMessage – Sends the specified message to a window or windows. The SendMessage function calls the window procedure for the specified window …

C#

C# | Put time consuming operations in thread

Assuming that we have a time consuming operation in a function called TimeConsOperation. To execute it in a separated thread do the following: private void someFunction() { Thread _thread = new Thread(new ThreadStart(TimeConsOperation)); _thread.Start(); }   private void TimeConsOperation() { for(;;) { .. } }private void someFunction() { Thread _thread = new Thread(new ThreadStart(TimeConsOperation)); _thread.Start(); …