| Subcribe via RSS

Find All Cookie Values in a Given Application via ASP.Net and c#

January 14th, 2010 | Comments Off | Posted in ASP.NET, C#

Cookies can be especially maddening at times if you don’t know what values are being stored within them. The following simple script is written in csharp and ASP.Net and allows you to see all of the cookies currently hanging around under a given application. Pretty handy when trying to fish yourself out of a problem.

Subtracting Dates in C#

December 16th, 2009 | Comments Off | Posted in C#
I’ve got an exam application that has a fixed time limit and relays the clock winding down to the test taker via an AJAX call. So we know what time it is now (DateTime.Now), we know when we started the exam (set value in session) and we know the time limit of the exam (pulled from the database). How do we bring it all together? We have to figure out our finish time by running the AddMinutes method against our exam beginning DateTime value, adding our predefined number of minutes set in the time limit. Next, we simply subtract our begin time from our finish time, and that leaves us with a TimeSpan value. The only thing remaining is to do something with that TimeSpan value. It has a host of methods for conversion, but we are going to run the TotalMinutes method this go around to get us the countdown value. Maybe not as simple as VB’s datediff, but simple enough.

Get User Name for NT Authentication in C#

December 10th, 2009 | Comments Off | Posted in C#

Lots of systems sit behind an NT Authentication wall to keep the unwanted users out, but does it really do you any good if you don’t know which user made a given update to a system. Also this is good if you want to give a certain user administrative privledges, displaying certain functionality while hiding it from the rest of your users. The easiest way to get at that piece of information is to jump down into the System.Security.Principal.WindowsIdentity class and hit up the GetCurrent().Name method. Note this will pull back the user name preceeded by the domain they are logged onto so to ditch that you’ll just need to clean it up with a Regex statement (ex: Regex.Replace(sNTUser,”DOMAINNAME.”,”"))

Keep DOS Window from Closing on Console Applications

November 3rd, 2009 | Comments Off | Posted in C#
For those of you who develop console apps for automating day-to-day activities, it can be annoying when you are trying to see what happened in your execution block. One solution is to pull up a DOS prompt, navigate to the given folder, then trigger it to run. A second option is to add the line Console.WriteLine(); to the end of your code processing block. This basically tells your program that it can’t close up shop until you give the ok.

Remove the Last Character in a String via CSharp

September 2nd, 2009 | Comments Off | Posted in C#

I was compiling a string this afternoon with several options drawn from the database. I put in a hyphen to seperate each element to keep everything looking neat. It was something like this:

That is all nice and good and all, but it gives us one too many trailing hyphens like this:

Lettuce – Tomato – Onion -
So to get around this, we need to chop off that last character from the string so we have a clean string. The solution is very simple. We call the Remove() function and point it to one character before the end of our string (determined by calculating the length of the string). Consider that sucker removed.

Read from a File with CSharp (c#)

August 4th, 2009 | Comments Off | Posted in C#

I did the bookend post to this a couple weeks back (i.e. how to read from a file) so its time I wrapped this topic up. Where the writing function was StreamWriter, we instead use StreamReader to extract the text from the document. Pretty logical right? In the example below, I’m pulling a block of code out of a text file named code.txt so I can append it to the header of a document I’m getting ready to generate. So once I’ve told the StringReader which file we are working with, we need to loop through that file line by line to suck them into the reader. In this case I’m storing that output in a string variable I’ve called sCode. Finally, once we’ve captured all the file’s contents we close the StreamReader to release our resources. Don’t forget to make your reference to System.IO or Visual Studio is going to yell at you. Now I can take that sCode value and do with it what I will. Just like the StreamWriter class, we can accomplish a big task in a few lines of code, leaving more time to goof off looking for iPhone apps.

Tags: ,

Write to a File with CSharp (c#)

July 24th, 2009 | Comments Off | Posted in C#

This one is pretty self explanatory. You have some content you’ve gathered from a database, webservice, other means and you want to write that out to a file. We are going to call on the StreamWriter class for help. First things first, reference the System.IO class so you have all the libraries to make this work. Next, we define what the file is going to be called and where it is going to be written to. When I define the StreamWriter, I’m passing in three values 1) the file path, 2) whether I want to append the content to the existing file (true) or overwrite it (false), 3) and what form of encoding I’ll be using (UTF8 in the instance). Next, we go through and write each line out and flush it into the file. Five lines of code to take care of this indispensable function.

Capitalize First Letter of Each Word in a String via CSharp (c#)

July 10th, 2009 | Comments Off | Posted in C#

Sometimes you are parsing urls or other files and would like to be able to do something with that text. All lower case and boring isn’t going to excite many of your users. So in looking to capitalize the first letter of each word in a string we have to look beyond the bounds of the String class. The answer is in the globalization namespace. There is a method TextInfo.ToTitleCase that does just that.

That puny text becomes “Ajax The Definitive Guide.” The folks at O’Reilly would be happy that I properly emphasized their book’s title.

How to Find out if a Number is Even/Odd in C#

June 19th, 2009 | Comments Off | Posted in C#

There are several different ways at getting at this, but your simplist is to evaluate the number via the modulus operator (%). The modulus operator calculates the remainder when the first number is divided by the second. If its even, there won’t be a remainder and if its odd — you get the routine. How is this helpful? Say we are dynamically writing out rows in a datagrid and we need to tag them with alternating colors. This makes for an easy solution.

Tags: , ,

Writing Inline If Statements in CSharp

June 9th, 2009 | Comments Off | Posted in C#
Sometimes you don’t always have room to execute a full if statement block. Think about when you are getting back database output and processing it through the gridview. If you are looking to evaluate the returned value to display information to the user, you can either make a method call or do a quick inline if statement. It does everything a traditional if statement accomplishes, but its streamlined into one line. It follows the format ( ? : ). The following is the inline if statement in practice. Tags: ,