Category: C#

C#

WebRequest throws IOException “The specified registry key does not exist.”

When you are using .NET 2.0 or .NET 3.5 and you are trying to make a web request with WebRequest class you may see the following internal error if you have Common Language Runtime Exceptions enabled during debugging in Visual Studio: Exception thrown: ‘System.IO.IOException’ in mscorlib.dll Additional information: The specified registry key does not exist. …

C#

The key LocalizedPerfCounter does not exist

When you are using .NET 2.0 or .NET 3.5 and you are trying to make a web request with WebRequest class you may see the following internal error if you have Common Language Runtime Exceptions enabled during debugging in Visual Studio: Exception thrown: ‘System.InvalidOperationException’ in System.dll Additional information: The key ‘LocalizedPerfCounter’ does not exist in …

C#, Programming

RSA 2048 using CryptoAPI and C#

RSACryptoServiceProvider performs asymmetric encryption and decryption using the implementation of the RSA algorithm provided by the cryptographic service provider (CSP). – MSDN Generate Public and Private Keys public static void GenerateKeys(out string PublicKey, out string PrivateKey) { using (RSACryptoServiceProvider RSA2048 = new RSACryptoServiceProvider(2048)) { PublicKey = RSA2048.ToXmlString(false); PrivateKey = RSA2048.ToXmlString(true); } }public static void GenerateKeys(out …

C#, Programming

AES 256 and Base64 using CryptoAPI and C#

Rfc2898DeriveBytes implements password-based key derivation functionality, PBKDF2, by using a pseudo-random number generator based on HMACSHA1. – MSDN Generate salt and key private static string GenerateSaltKey(string password) { Rfc2898DeriveBytes rfc2898db = new Rfc2898DeriveBytes(password, 16, 10000);   byte[] data = new byte[48]; Buffer.BlockCopy(rfc2898db.Salt, 0, data, 0, 16); Buffer.BlockCopy(rfc2898db.GetBytes(32), 0, data, 16, 32); return Convert.ToBase64String(data); }   …

C#

A full implementation of the VirusTotal 2.0 API

VirusTotal.NET – A full implementation of the VirusTotal 2.0 API. Here you will find project’s source code. Features Based on RestSharp (http://restsharp.org) to deserialize the VirusTotal JSON into objects Scan, rescan and get report of scanned files Scan websites and files Support for HTTP and HTTPS Support for checking if files have been scanned before …

C#

Get Windows Users with C# and WMI

class SystemUser { public enum AccountType { DUPLICATE_ACCOUNT = 256, NORMAL_ACCOUNT = 512, INTERDOMAIN_TRUST_ACCOUNT = 2048, WORKSTATION_TRUST_ACCOUNT = 4096, SERVER_TRUST_ACCOUNT = 8192 };   public AccountType Type { get; set; } public string Caption { get; set; } public string Description { get; set; } public bool Disabled { get; set; } public string Domain …

C#

Update NoIP Host with c#

More about NoIP dynamic dns update request click here. private void UpdateNoIPHost() { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create( string.Format("http://dynupdate.no-ip.com/nic/update?hostname={0}", "<NoIP Hostname>")); request.Method = "GET"; request.Proxy = null; request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64)"; request.Timeout = 10000; request.Headers.Add(HttpRequestHeader.Authorization, string.Format("Basic {0}", Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", "<NoIP username>", "<NoIP password>")))));   using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { } } …

C#

Check if user is Idle with C#

GetLastInputInfo function – Retrieves the time of the last input event. LASTINPUTINFO structure – Contains the time of the last input. public bool IsUserIdle() { uint idleTime = (uint)Environment.TickCount – GetLastInputEventTickCount(); idleTime = ((idleTime > 0) ? (idleTime / 1000) : 0); //user is idle for 5 min return (idleTime >= 5 * 60); } …

C#

Is64BitOperatingSystem for C# and .Net 2.0

public bool Is64BitOperatingSystem() { try { string osArch = string.Empty; string query = String.Format("SELECT * FROM Win32_OperatingSystem");   using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query)) { foreach (ManagementObject mObject in searcher.Get()) { osArch = mObject["OSArchitecture"].ToString(); break; } }   return (osArch != "64-bit") ? false : true; } catch { }   return false; }public bool …

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 …