Tag: c#

C#

Get all system processes with C#

List all System running processes with the following code: public void GetAllProcesses() { foreach (System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcesses()) { try { Console.WriteLine( proc.Id + " " + proc.ProcessName + " " + proc.PriorityClass.ToString() + " " + proc.MainModule.FileName); } catch (Exception) { //… } } }public void GetAllProcesses() { foreach (System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcesses()) { …

C#

Get a list of opened windows C#

With the following method you can list all opened windows using C#. public void ListOpenedWindows() { IntPtr hwnd; foreach (System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcesses()) { if ((hwnd = proc.MainWindowHandle) != IntPtr.Zero) Console.WriteLine(hwnd.ToString() + " " + proc.MainWindowTitle); } }public void ListOpenedWindows() { IntPtr hwnd; foreach (System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcesses()) { if ((hwnd = proc.MainWindowHandle) != IntPtr.Zero) …

C#

XML Serialization of C# Objects

To serialize a C# object to XML you need XmlSerializer class. 1. Create a Customer class for example [Serializable, XmlRoot("Customer"), XmlType("Customer")] public class Customer { private String _name; private String _address; private String _tel; private String _fax; private String _vat_registration_number; private String _tax_office; private String _id;   /// <summary> /// /// </summary> public Customer() { …

General

Convert String to byte array in C#

Converting a byte array to string (unicode): byte[] b = Encoding.Unicode.GetBytes("unicode characters"); String s = Encoding.Unicode.GetString(b);byte[] b = Encoding.Unicode.GetBytes("unicode characters"); String s = Encoding.Unicode.GetString(b); Converting a byte array to string (ascii): byte[] b = Encoding.Unicode.GetBytes("ascii characters"); String s = Encoding.Unicode.GetString(b);byte[] b = Encoding.Unicode.GetBytes("ascii characters"); String s = Encoding.Unicode.GetString(b);

C#

Convert byte array to String in C#

Assuming that byte array contains data in Unicode format: byte[] data = ….; String data_s = Encoding.Unicode.GetString(data);byte[] data = ….; String data_s = Encoding.Unicode.GetString(data); Assuming that byte array contains data in ASCII format: byte[] data = ….; String data_s = Encoding.ASCII.GetString(data);byte[] data = ….; String data_s = Encoding.ASCII.GetString(data);

C#

Connecting MS Access Database in C#

Let’s how we can connect to an MS Access database (accdb) in C#. String _database_path = "path/to/database/file"; String _conn_str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + _database_path + ";Persist Security Info=False";   //connect OleDbConnection _connection = new OleDbConnection(_conn_str); _connection.Open();   //disconnect _connection.Close();String _database_path = "path/to/database/file"; String _conn_str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + _database_path + ";Persist Security Info=False"; //connect OleDbConnection …

C#

Hide console Window in C#

To hide console window in a C# console application your Program.cs file should be similar to this: namespace mynamespace { class Program { [DllImport("kernel32.dll")] static extern IntPtr GetConsoleWindow();   [DllImport("user32.dll")] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);   const int SW_HIDE = 0; const int SW_SHOW = 5;   static void Main(string[] args) { var …

C#

Show elapsed time on a Windows form in C#

We are going to use the Stopwatch class and a Timer. First of all we have to add two buttons, a label, named mylabel, to show elapsed time and a Timer component, named mytimer, in our form and set Timer’s interval value to 1000 milliseconds (1 second). private Stopwatch stopWatch;private Stopwatch stopWatch; One button will …

C#

Banner grabbing with C#

TcpClient client = new TcpClient("http://mywebsite.com", 80); client.SendTimeout = 2000; client.ReceiveTimeout = 2000; NetworkStream ns = client.GetStream(); StreamWriter sw = new StreamWriter(ns); sw.Write("HEAD / HTTP/1.1rn" + "User-Agent: Test agent 1.0rn" + "Host: www.example.orgrn" + "Connection: Closernrn"); sw.Flush();   byte[] bytes = new byte[2048]; int bytesRead = ns.Read(bytes, 0, bytes.Length); String response = Encoding.ASCII.GetString(bytes, 0, bytesRead);TcpClient client …

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(); …