Tag: c#

C#, Hacking

Perform click event on another application using C#

We will need the following methods: SendMessage, FindWindowEx, GetWindowTextLength, GetWindowText, EnumChildWindows pinvoke.net SendMessage [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);[DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam); FindWindowEx [DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string …

C#

How to make a Http Web Request properly using C#

//.. private HttpWebRequest _httpwebrequest; //..//.. private HttpWebRequest _httpwebrequest; //.. private void button1_Click(object sender, EventArgs e) { Thread t = new Thread(new ThreadStart(Test)); t.IsBackground = true; t.Start(); }private void button1_Click(object sender, EventArgs e) { Thread t = new Thread(new ThreadStart(Test)); t.IsBackground = true; t.Start(); } public void Test() { Dictionary<String, String> dict = new Dictionary<String, String>(); …

C#

FTP Uploading with C#

void ftpUpload(String filename, String ftpserver, String username, String password) { try { FileInfo file = new FileInfo(filename); FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(ftpserver));   request.Method = WebRequestMethods.Ftp.UploadFile; request.UseBinary = true; request.UsePassive = true; request.KeepAlive = true; request.Credentials = new NetworkCredential(username, password); request.ContentLength = file.Length; int buffer_length = 16 * 1024; byte[] buffer = new byte[file.Length]; FileStream …

C, C#

Get Active Window Title

Get Active Window Title in C: #include <windows.h> #include <stdio.h>   int main() { char title[256] = ""; HWND handle = GetForegroundWindow(); if (handle) { GetWindowText(handle, title, 256); printf("%srn",title); } else { printf("not foundrn"); } return 0; }#include <windows.h> #include <stdio.h> int main() { char title[256] = ""; HWND handle = GetForegroundWindow(); if (handle) { …

C#

How to terminate a process in c#

By name foreach (Process p in Process.GetProcessesByName("process_name")) { try { p.Kill(); p.WaitForExit(); } catch { } }foreach (Process p in Process.GetProcessesByName("process_name")) { try { p.Kill(); p.WaitForExit(); } catch { } } By id try { Process p = Process.GetProcessById(process_id); p.Kill(); p.WaitForExit(); } catch { }try { Process p = Process.GetProcessById(process_id); p.Kill(); p.WaitForExit(); } catch { …

C#

Calculate SHA1 hash from String in C#

Calculate SHA1 hash with this function. public String SHA1(String plaintext) { try { System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create(); byte[] bytes = System.Text.Encoding.ASCII.GetBytes(plaintext); byte[] hash = sha1.ComputeHash(bytes);   StringBuilder sb = new StringBuilder(); for (int i = 0; i < hash.Length; i++) sb.Append(hash[i].ToString("x2"));   return sb.ToString(); } catch { return null; } }public String SHA1(String plaintext) { …

C#

Append files to zip file with C#

Open an existing zip file and append files with C# and DotNetZip library. public void AppendFile(String Filename, Zipfilename){ if (File.Exists(Zipfilename)) { ZipFile _zipfile = ZipFile.Read(Zipfilename); try { _zipfile.AddFile(Filename); } catch { } _zipfile.Save(); _zipfile = null; } }public void AppendFile(String Filename, Zipfilename){ if (File.Exists(Zipfilename)) { ZipFile _zipfile = ZipFile.Read(Zipfilename); try { _zipfile.AddFile(Filename); } catch { …

C#

Get system hard drives with C#

Retrieve the available disk drives with GetLogicalDrives function. try { foreach (String lodi in Environment.GetLogicalDrives()) if (lodi != String.Empty) Console.WriteLine(lodi); } catch (Exception) { //… }try { foreach (String lodi in Environment.GetLogicalDrives()) if (lodi != String.Empty) Console.WriteLine(lodi); } catch (Exception) { //… }

C#

Get system services with C#

To list system services, we are going to use ServiceController class. Add a reference to System.ServiceProcess namespace first. try { foreach (System.ServiceProcess.ServiceController s in System.ServiceProcess.ServiceController.GetServices()) Console.WriteLine(s.Status + " — " + s.ServiceName); } catch (Exception) { //… }try { foreach (System.ServiceProcess.ServiceController s in System.ServiceProcess.ServiceController.GetServices()) Console.WriteLine(s.Status + " — " + s.ServiceName); } catch (Exception) { …

C#

Get all opened windows in C#

With the following code you can get all opened windows for the current user. public void GetOpenWindows() { IntPtr hwnd; try { foreach (System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcesses()) if ((hwnd = proc.MainWindowHandle) != IntPtr.Zero) Console.WriteLine(hwnd.ToString() + " " + proc.MainWindowTitle); } catch (Exception) { } }public void GetOpenWindows() { IntPtr hwnd; try { foreach (System.Diagnostics.Process proc …