C# Faster way to truncate a string

Very often I found this problem and now I got a solution! I’ve compared some methods and I was decided which one. But I verified time (if you want to know how, read this post) of them and the final solutions are: static string TruncateLongString(string str, int maxLength) { return str.Substring(0, Math.Min(str.Length, maxLength)); } or string TruncateString(string str, int maxLength) { return new string(str.Take(maxLength).ToArray()); } The best solution than even is the second one!

Read More

String to Hex – Hex to String Convert

You can convert a string to hex or vice-versa with this methods. Example for real world; You want set / get some data from URL, but if your data has some special chars (like ^$ %) it’ll be problem… In this case, you can use this methods and convert your data to hex. public string ConvertStringToHex(string asciiString) { string hex = ""; foreach (char c in asciiString) { int tmp = c; hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString())); } return hex; } public string ConvertHexToString(string HexValue) { string StrValue = ""; while…

Read More

Regular expression for UK postcode

I’m looking for an answer to my regular expresssion problem in c#. I’m looking for a match on a specific postcode format and have run into problems. Here you can find my solution with the regex pattern. C# public bool IsPostCode (string postcode) { return ( Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][0-9][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)”) || Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][0-9][0-9][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)”) || Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][A-HK-Ya-hk-y][0-9][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)”) || Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][A-HK-Ya-hk-y][0-9][0-9][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)”) || Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][0-9][A-HJKS-UWa-hjks-uw][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)”) || Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][A-HK-Ya-hk-y][0-9][A-Za-z][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z{2}$)”) || Regex.IsMatch(postcode, “(^[Gg][Ii][Rr][]*0[Aa][Aa]$)”) ); } Visual Basic Public Function IsPostCode(postcode As String) As Boolean Return (Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][0-9][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)”) OrElse Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][0-9][0-9][…

Read More

OWIN and Facebook: the developers of this app have not set up this app properly for Facebook Login?

Did you received this error when you try to login in your Owin app with Facebook? App Not Set Up: This app is still in development mode, and you don’t have access to it. Switch to a registered test user or ask an app admin for permissions. Solution Go to https://developers.facebook.com/ Click on the My Apps menu on the top bar and select your appThe circle next to your app name is not fully green. When you hover mouse on it, you’ll see a popup saying, "Not available to all…

Read More

C# ASP.NET MVC OWIN and Twitter authentication error

We have an MVC project using OWIN Framework to allow our users to authenticate using Twitter. However starting today, we have been getting this exception when trying to authenticate: System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure. Thanks to the power of open source we can see that the thumbprints for the twitter certificates have been coded in the Katana Project. Microsoft.Owin.Security.Twitter.TwitterAuthenticationOptions Recently some certificates must have changed and now the thumbprints no longer match. Please add a new thumb print for the "VeriSign Class 3 Public…

Read More

ASP.NET MVC Return image dynamically drawn in controller

If you have problem returning dynamically drawn image from controller below informations may help. The task looks easy and should be achieved with this code: public ActionResult Image(string text) { //Create new image Image img = new Bitmap(100, 50); Graphics g = Graphics.FromImage(img); //Do some drawing Font font = new Font("Arial", 24); PointF drawingPoint = new PointF(10, 10); g.DrawString(text, font, Brushes.Black, drawingPoint); //Return Image MemoryStream ms = new MemoryStream(); img.Save(ms, ImageFormat.Png); return new FileStreamResult(ms, "image/png"); }   Unfortunately this won’t work. At least not in .NET 4.0. Perceptive person will…

Read More

ASP.NET MVC OWIN and Microsoft account

Register an app in the Microsot Account Developer Center Go to the Microsoft Account Developer Center and create a new application. After you have registered the application take note of the App ID and App Secret: Install the Nuget Package Install the Nuget Package which contains the Microsoft OAuth provider. Install-Package Microsoft.Owin.Security.MicrosoftAccount Register Provider Locate the file in your project called \App_Start\Startup.Auth.cs. Ensure that you have imported the Owin namespace: using Owin; In the ConfigureAuth method add the following lines of code: app.UseMicrosoftAccountAuthentication( clientId: "Your client ID", clientSecret: "Your client…

Read More

Microsoft cuts off Windows 8’s security updates on January 12

Windows 8 is about to get a lot less secure. After January 12, Microsoft will stop offering security patches for the three-year-old operating system. Users will have to upgrade to either Windows 8.1 or Windows 10 to keep receiving updates. As Ed Bott notes Windows 8 is an exception to Microsoft’s typical support lifecycle policy, which provides 10 years of security fixes after the initial launch date. That’s because Microsoft considers Windows 8.1—a meaty update released nearly one year after Windows 8—to be a service pack, rather than an entirely…

Read More

Support Ending for the .NET Framework 4.0, 4.5 and 4.5.1 on Tuesday

In less than a week Microsoft will formally end support for versions 4.0, 4.5, and 4.5.1 of the .NET Framework. Users should upgrade to a later version such as the slightly incompatible .NET 4.5.2. Before we move on, it should be noted that this only affects the 4.x series. The much older .NET 3.5 SP 1 will continue to be supported. In this context, support means having access to technical support, security updates, and hotfixes. Compatibility When upgrading to .NET 4.5.2, ASP.NET developers may see a compatibility issue. Though considered…

Read More

Microsoft is pulling the plug on Internet Explorer 8, 9, and 10 next Tuesday

Microsoft is ending support for Internet Explorer 8, 9, and 10 next week on January 12th, releasing a final patch encouraging users to upgrade to one of the company’s more recent browsers. The end of support means that these older versions of Internet Explorer will no longer receive security updates or technical support, making anyone who uses them much more vulnerable to hackers. A recently-announced patch will deliver the last few bug fixes, as well as an "End of Life" notification telling users to upgrade to IE 11 or Microsoft…

Read More

The current type, is an interface and cannot be constructed. Are you missing a type mapping?

You might have missed to register your Interface and class (which implements that inteface) registration in your code. e.g if the error is "The current type, xyznamespace. Imyinterfacename, is an interface and cannot be constructed. Are you missing a type mapping?" Then you must register the class which implements the Imyinterfacename in the UnityConfig class in the Register method. Using the following code as an example using System; using Microsoft.Practices.Unity; using Microsoft.Practices.Unity.Configuration; using PSC.Shorturl.Web.Business; using PSC.Shorturl.Web.Business.Implementations; namespace PSC.Shorturl.Web.App_Start { /// /// Specifies the Unity configuration for the main container. ///…

Read More

Microsoft CEO Satya Nadella explains why it doesn’t matter if only a handful of people have a Windows phone

Microsoft CEO Satya Nadella has explained in an interview with BuzzFeed why the company is not concerned about the market share of its mobile operating system, which is about 1.7% globally. He said the company would be doing a “disservice” to itself by measuring its success solely by market share. “If you think of this more like a graph,” he said of how people use products, “these [devices] are all nodes. Sometimes the user will use all of these devices … sometimes they’ll use only one or two of our…

Read More

Automatic Table of Contents

Any long page of content with distinct and well marked up content can benefit from a table to contents. A table of contents provides a quick way to jump down the page to the desired section. Of course you can create a table of contents manually, but it may be smart to build it dynamically on-the-fly with JavaScript. This is true for several reasons: It’s easier – write the JavaScript once and it can create the Table on Contents on every page you need it. It’s more reliable – the…

Read More

What’s New in Visual Studio Update 1 for .NET Managed Languages

Hold on to your hats, cowboys and cowgirls! A lot of exciting things are coming out of the .NET Managed Languages team for Visual Studio 2015 Update 1. Read on to learn more about new IDE features, interactive C#, new code analysis management, Visual F# improvements, and the new F5 experience for Roslyn open source development. New Editor Features Now that we have Roslyn in Visual Studio 2015, we can leverage its power to create the smartest IDE out there. The improvements we’ve made to the IDE experience in Update…

Read More

Google Launches Android Studio 2.0 With Improved Android Emulator And New Instant Run Feature

Google today launched version 2.0 of its Android Studio integrated development environment (IDE) for writing apps for its mobile operating system. Android Studio, which is based on IntelliJ, launched back in 2013 and came out of beta a year ago. It includes everything a developer needs to build an app, including a code editor, code analysis tools, emulators for all of Google’s Android platforms, and more. The new version is now available as a preview in the Canary release channel of Android Studio. With version 2.0, as Google’s group product…

Read More

How to generate a random password

These code samples demonstrate how to generate a strong random password, which does not contain ambiguous characters (such as [1,l,I] or [O,0]). A strong password must be at least eight characters long and contain a combination of lower- and upper-case letters, numbers, and special characters. /////////////////////////////////////////////////////////////////////////////// // SAMPLE: Generates random password, which complies with the strong password // rules and does not contain ambiguous characters. // // To run this sample, create a new Visual C# project using the Console // Application template and replace the contents of the Class1.cs…

Read More

How to use REPLACE() within NTEXT columns in SQL Server

SQL has an incredibly useful function, REPLACE(), which replaces all occurrences of a specified string with another string, returning a new string. It works great with all forms of NCHAR and NVARCHAR fields. It does not, however, work with NTEXT fields. Fear not — there’s an easy workaround, thanks to type-casting and SQL 2005’s NVARCHAR(max) datatype. Here’s the process in an nutshell. Cast the NTEXT field to the NVARCHAR(max) datatype using the CAST function. Perform your REPLACE on the output of #1. Cast the output of #2 back to NTEXT.…

Read More

Using iTextSharp to generate pdf file in asp.net

One of the common requirement of web applications is to provide users a way to download some contents. In case of a report, almost all of the reporting tools have the export to pdf kind of function available. But sometime, it is not a report that needs to be generated in pdf. But it could be simple web page or content of a page or sometimes a plain text needs to be generated in pdf format. There are many third party tools are available for this task. We can import…

Read More

Happy 60th Birthday Bill Gates

The founder of Microsoft and wealthiest man in the world turns 60 years old today. William Henry Gates III was born October 28th, 1955 in Seattle, Washington where he still resides in his post Microsoft years. He has much to celebrate as he turns 60. After leading Microsoft for decades it must be quite rewarding to see the company he founded grow in such a bold direction this year with a booming commercial cloud computing division, new category defining hardware that is leading the competition, and an incredibly fast adoption…

Read More

Microsoft’s new Windows 10 build allows you to text from your PC, but it’s the bug fixes that impress

Unfortunately, it requires an upgrade from Windows Phone 8.1 – again. Microsoft launched build 10572 of Windows 10 Mobile to its insiders, together with a few nifty improvements on the messaging fronts. But, as before, you’ll still need to first downgrade to Windows Phone 8.1 to take advantage. Microsoft did say, however, that this two-steps-back, one-step-ahead approach will soon stop, and users in the Windows Insider program will once again be able to upgrade from Windows 10 preview builds without having to downgrade first. But the new build offers several…

Read More

Write C#. Run JavaScript.

Open Source C# to JavaScript Compiler and Frameworks. Run Your App On Any Device Using JavaScript. Use Bridge.NET to build platform independent applications for mobile, web and desktop. Run on iOS, Windows, Mac, Linux and billions of other devices with JavaScript support. Compile your C# code into native JavaScript and deploy on Billions of devices. Try it on bridge.net or fork it on GitHub

Read More

How do I get a Unique Identifier for a Device within Windows 10 Universal?

If you google a bit about this problem, you can’t find a right solution because all people are speaking about Hardware Token. Unfortunately this functionality doesn’t exists for Universal Windows Application. There are at the moment only a way. You have to add the Extension reference “Windows Desktop Extensions for the UWP” or “Windows Mobile Extensions for the UWP“, then you can use the following code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Windows.Security.ExchangeActiveSyncProvisioning; using Windows.System.Profile; namespace PSC.Code { public sealed class DeviceInfo { private static…

Read More