How do I send ctrl+c to a process in c#?

Despite the fact that using GenerateConsoleCtrlEvent() for sending Ctrl+C signal is the right answer, it needs significant clarification to get it to work in different .NET application types. If your .NET application doesn’t use its own console (Windows Forms/WPF/Windows Service/ASP.NET), the basic flow is: Attach the main .NET process to the console of the process … Read more

datetime.parse and making it work with a specific format

DateTime.ParseExact(input,”yyyyMMdd HH:mm”,null); assuming you meant to say that minutes followed the hours, not seconds – your example is a little confusing. The ParseExact documentation details other overloads, in case you want to have the parse automatically convert to Universal Time or something like that. As @Joel Coehoorn mentions, there’s also the option of using TryParseExact, … Read more

Workaround for lack of ‘nameof’ operator in C# for type-safe databinding?

This code basically does that: class Program { static void Main() { var propName = Nameof<SampleClass>.Property(e => e.Name); Console.WriteLine(propName); } } public class Nameof<T> { public static string Property<TProp>(Expression<Func<T, TProp>> expression) { var body = expression.Body as MemberExpression; if(body == null) throw new ArgumentException(“‘expression’ should be a member expression”); return body.Member.Name; } } (Of course … Read more

LINQ on the .NET 2.0 Runtime

It’s weird that no one has mentioned LINQBridge. This little awesome project is a backport of LINQ (IEnumerable, but without IQueryable) and its dependencies (Func, Action, etc) to .NET 2.0. And: If your project references LINQBridge during compilation, then it will bind to LINQBridge’s query operators; if it references System.Core during compilation, then it will … Read more

How to create a simple proxy in C#?

I wouldn’t use HttpListener or something like that, in that way you’ll come across so many issues. Most importantly it’ll be a huge pain to support: Proxy Keep-Alives SSL won’t work (in a correct way, you’ll get popups) .NET libraries strictly follows RFCs which causes some requests to fail (even though IE, FF and any … Read more

Compression/Decompression string with C#

The code to compress/decompress a string public static void CopyTo(Stream src, Stream dest) { byte[] bytes = new byte[4096]; int cnt; while ((cnt = src.Read(bytes, 0, bytes.Length)) != 0) { dest.Write(bytes, 0, cnt); } } public static byte[] Zip(string str) { var bytes = Encoding.UTF8.GetBytes(str); using (var msi = new MemoryStream(bytes)) using (var mso = … Read more

Can I control the location of .NET user settings to avoid losing settings on application upgrade?

I wanted to add this quoted text as a reference for when i have this problem in the future. Supposedly you can instruct the ApplicationSettings infrastructure to copy settings from a previous version by calling Upgrade: Properties.Settings.Value.Upgrade(); From Client Settings FAQ blog post: (archive) Q: Why is there a version number in the user.config path? … Read more

How do I get and set Environment variables in C#?

Use the System.Environment class. The methods var value = System.Environment.GetEnvironmentVariable(variable [, Target]) and System.Environment.SetEnvironmentVariable(variable, value [, Target]) will do the job for you. The optional parameter Target is an enum of type EnvironmentVariableTarget and it can be one of: Machine, Process, or User. If you omit it, the default target is the current process.