How to Get Active Process Name in C#?

As mentioned in this answer, you have to use GetWindowThreadProcessId() to get the process id for the window and then you can use the Process: [DllImport(“user32.dll”)] public static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint ProcessId); [DllImport(“user32.dll”)] private static extern IntPtr GetForegroundWindow(); string GetActiveProcessFileName() { IntPtr hwnd = GetForegroundWindow(); uint pid; GetWindowThreadProcessId(hwnd, out pid); Process p … Read more

Update UI from thread in WinRT

The preferred way to deal with this in WinRT (and C# 5 in general) is to use async–await: private async void Button_Click(object sender, RoutedEventArgs e) { string text = await Task.Run(() => Compute()); this.TextBlock.Text = text; } Here, the Compute() method will run on a background thread and when it finishes, the rest of the … Read more

WPF + MVVM + RadioButton : How to handle binding with single property?

You need a IValueConverter. //define this in the Window’s Resources section or something similiarly suitable <local:GenderConverter x:Key=”genderConverterKey” /> <RadioButton Content=”M” IsChecked=”{Binding Gender, Converter={StaticResource ResourceKey=genderConverterKey}, ConverterParameter=M}” /> <RadioButton Content=”F” IsChecked=”{Binding Gender, Converter={StaticResource ResourceKey=genderConverterKey}, ConverterParameter=F}” /> The converter public class GenderConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return ((string)parameter … Read more

How write a file using StreamWriter in Windows 8?

Instead of StreamWriter you would use something like this: StorageFolder folder = ApplicationData.Current.LocalFolder; StorageFile file = await folder.CreateFileAsync(); using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.ReadWrite)) { using (IOutputStream outputStream = fileStream.GetOutputStreamAt(0)) { using (DataWriter dataWriter = new DataWriter(outputStream)) { //TODO: Replace “Bytes” with the type you want to write. dataWriter.WriteBytes(bytes); await dataWriter.StoreAsync(); dataWriter.DetachStream(); } await outputStream.FlushAsync(); … Read more

How do I lock a windows workstation programmatically? [duplicate]

I haven’t tried it myself, but I found this on google Process.Start(@”C:\WINDOWS\system32\rundll32.exe”, “user32.dll,LockWorkStation”); edit: I tried it, and it works! edit2: Here’s a solution using user32.dll that doesn’t start an external process. using System.Runtime.InteropServices; declare a method like this: [DllImport(“user32.dll”)] public static extern bool LockWorkStation(); and then call LockWorkStation();. VoilĂ 

How do I use Selenium in C#?

From the Selenium Documentation: using OpenQA.Selenium.Firefox; using OpenQA.Selenium; class GoogleSuggest { static void Main(string[] args) { IWebDriver driver = new FirefoxDriver(); //Notice navigation is slightly different than the Java version //This is because ‘get’ is a keyword in C# driver.Navigate().GoToUrl(“http://www.google.com/”); IWebElement query = driver.FindElement(By.Name(“q”)); query.SendKeys(“Cheese”); System.Console.WriteLine(“Page title is: ” + driver.Title); driver.Quit(); } }

How to simulate browser HTTP POST request and capture result in C#

You could take a look at the WebClient class. It allows you to post data to an arbitrary url: using (var client = new WebClient()) { var dataToPost = Encoding.Default.GetBytes(“param1=value1&param2=value2”); var result = client.UploadData(“http://example.com”, “POST”, dataToPost); // do something with the result } Will generate the following request: POST / HTTP/1.1 Host: example.com Content-Length: 27 … Read more