Check if a string contains an element from a list (of strings)

With LINQ, and using C# (I don’t know VB much these days): bool b = listOfStrings.Any(s=>myString.Contains(s)); or (shorter and more efficient, but arguably less clear): bool b = listOfStrings.Any(myString.Contains); If you were testing equality, it would be worth looking at HashSet etc, but this won’t help with partial matches unless you split it into fragments … Read more

Difference between Lookup() and Dictionary(Of list())

Two significant differences: Lookup is immutable. Yay 🙂 (At least, I believe the concrete Lookup class is immutable, and the ILookup interface doesn’t provide any mutating members. There could be other mutable implementations, of course.) When you lookup a key which isn’t present in a lookup, you get an empty sequence back instead of a … Read more

Best way in asp.net to force https for an entire site?

Please use HSTS (HTTP Strict Transport Security) from http://www.hanselman.com/blog/HowToEnableHTTPStrictTransportSecurityHSTSInIIS7.aspx <?xml version=”1.0″ encoding=”UTF-8″?> <configuration> <system.webServer> <rewrite> <rules> <rule name=”HTTP to HTTPS redirect” stopProcessing=”true”> <match url=”(.*)” /> <conditions> <add input=”{HTTPS}” pattern=”off” ignoreCase=”true” /> </conditions> <action type=”Redirect” url=”https://{HTTP_HOST}/{R:1}” redirectType=”Permanent” /> </rule> </rules> <outboundRules> <rule name=”Add Strict-Transport-Security when HTTPS” enabled=”true”> <match serverVariable=”RESPONSE_Strict_Transport_Security” pattern=”.*” /> <conditions> <add input=”{HTTPS}” pattern=”on” ignoreCase=”true” … Read more

.NET Global exception handler in console application

No, that’s the correct way to do it. This worked exactly as it should, something you can work from perhaps: using System; class Program { static void Main(string[] args) { System.AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper; throw new Exception(“Kaboom”); } static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e) { Console.WriteLine(e.ExceptionObject.ToString()); Console.WriteLine(“Press Enter to continue”); Console.ReadLine(); Environment.Exit(1); } } Do keep … Read more

.Net Data structures: ArrayList, List, HashTable, Dictionary, SortedList, SortedDictionary — Speed, memory, and when to use each? [closed]

Off the top of my head: Array* – represents an old-school memory array – kind of like a alias for a normal type[] array. Can enumerate. Can’t grow automatically. I would assume very fast insert and retrival speed. ArrayList – automatically growing array. Adds more overhead. Can enum., probably slower than a normal array but … Read more

How do I convert Word files to PDF programmatically? [closed]

Use a foreach loop instead of a for loop – it solved my problem. int j = 0; foreach (Microsoft.Office.Interop.Word.Page p in pane.Pages) { var bits = p.EnhMetaFileBits; var target = path1 +j.ToString()+ “_image.doc”; try { using (var ms = new MemoryStream((byte[])(bits))) { var image = System.Drawing.Image.FromStream(ms); var pngTarget = Path.ChangeExtension(target, “png”); image.Save(pngTarget, System.Drawing.Imaging.ImageFormat.Png); } … Read more

HTML encoding issues – “” character showing up instead of ” “

Somewhere in that mess, the non-breaking spaces from the HTML template (the  s) are encoding as ISO-8859-1 so that they show up incorrectly as an “” character That’d be encoding to UTF-8 then, not ISO-8859-1. The non-breaking space character is byte 0xA0 in ISO-8859-1; when encoded to UTF-8 it’d be 0xC2,0xA0, which, if you (incorrectly) … Read more

What is the difference between And and AndAlso in VB.NET?

The And operator evaluates both sides, where AndAlso evaluates the right side if and only if the left side is true. An example: If mystring IsNot Nothing And mystring.Contains(“Foo”) Then ‘ bla bla End If The above throws an exception if mystring = Nothing If mystring IsNot Nothing AndAlso mystring.Contains(“Foo”) Then ‘ bla bla End … Read more