Sorting Directory.GetFiles()

If you’re interested in properties of the files such as CreationTime, then it would make more sense to use System.IO.DirectoryInfo.GetFileSystemInfos(). You can then sort these using one of the extension methods in System.Linq, e.g.: DirectoryInfo di = new DirectoryInfo(“C:\\”); FileSystemInfo[] files = di.GetFileSystemInfos(); var orderedFiles = files.OrderBy(f => f.CreationTime); Edit – sorry, I didn’t notice … Read more

Best way to encode text data for XML

Depending on how much you know about the input, you may have to take into account that not all Unicode characters are valid XML characters. Both Server.HtmlEncode and System.Security.SecurityElement.Escape seem to ignore illegal XML characters, while System.XML.XmlWriter.WriteString throws an ArgumentException when it encounters illegal characters (unless you disable that check in which case it ignores … Read more

Redirect Trace output to Console

You can add the following to your exe’s .config file. <?xml version=”1.0″?> <configuration> <system.diagnostics> <trace autoflush=”true”> <listeners> <add name=”logListener” type=”System.Diagnostics.TextWriterTraceListener” initializeData=”cat.log” /> <add name=”consoleListener” type=”System.Diagnostics.ConsoleTraceListener”/> </listeners> </trace> </system.diagnostics> </configuration> I included the TextWriter as well, in case you’re interested in logging to a file.

.net: System.Web.Mail vs System.Net.Mail

System.Web.Mail is not a full .NET native implementation of the SMTP protocol. Instead, it uses the pre-existing COM functionality in CDONTS. System.Net.Mail, in contrast, is a fully managed implementation of an SMTP client. I’ve had far fewer problems with System.Net.Mail as it avoids COM hell.

Can .NET source code hard-code a debugging breakpoint?

You probably are after something like this: if(System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break(); Of course that will still get compiled in a Release build. If you want it to behave more like the Debug object where the code simply doesn’t exist in a Release build, then you could do something like this: // Conditional(“Debug”) means that calls to DebugBreak … Read more

How to use Reflection to Invoke an Overloaded Method in .NET

You have to specify which method you want: class SomeType { void Foo(int size, string bar) { } void Foo() { } } SomeType obj = new SomeType(); // call with int and string arguments obj.GetType() .GetMethod(“Foo”, new Type[] { typeof(int), typeof(string) }) .Invoke(obj, new object[] { 42, “Hello” }); // call without arguments obj.GetType() … Read more

Difference between the System.Array.CopyTo() and System.Array.Clone()

The Clone() method returns a new array (a shallow copy) object containing all the elements in the original array. The CopyTo() method copies the elements into another existing array. Both perform a shallow copy. A shallow copy means the contents (each array element) contains references to the same object as the elements in the original … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)