Archive for the ‘C#’ Category
Objects Remaining in Memory
I was implementing an image resizer and I kept running into a problem where I kept getting error messages saying that the image file was in use even after I disposed of the object in memory (the last step was to remove the unresized image).
Calling object.Dispose() is just a suggestion to say “whenever you want, we don’t need this in memory anymore”. However, because it doesn’t get rid of it immediately, meaning that it is still being referenced which means that the file won’t be able to be deleted immediately.
In order to get around this, you need to call the garbage collector yourself to force the application to get rid of the object from memory.
The Code:
string dest = @"C:\"; FileInfo imageFile = new FileInfo(file); Image image = ResizeImage(Image.FromFile(file),size); // Save the file to the file system SaveAsJpeg(image, dest + imageFile.Name, 100); // We don't need the image in memory any more (suggest it to be deleted) image.Dispose(); // Call the garbage collector GC.Collect(); GC.WaitForPendingFinalizers(); // Delete the old file imageFile.Delete();
Don’t use File when you mean Directory
I was working on a small archiving task for a project and one of the steps is to move a folder from one location to another. Not thinking anything of it, I tried to use File.Exists(archiveFullPath) assuming that a directory would also be considered a file. But for every folder I tried it kept returning false despite them actually existing.
So then I tried switching it over to the similar function but under the Directory object (Directory.Exists(archiveFullPath)) and it worked like a charm, returning true for the specific directories which I knew existed.
To make a long story short, if you are trying to do anything with a directory, use the Directory object instead of the File because directories aren’t treated the same as files.
void Main(string[] args) { string path = @"C:\testing\"; string working = path + @"Working\"; string archive = path + @"Archive\"; // Outputs: false Console.WriteLine(File.Exists(path)); // Outputs: false Console.WriteLine(File.Exists(working)); // Outputs: true Console.WriteLine(Directory.Exists(path)); // Outputs: true Console.WriteLine(Directory.Exists(working)); // Put the thread to sleep temporarily Thread.Sleep(100000); }
Hosting ASP.NET projects on a Linux Server
The other day, I was bored and was curious if whether or not I was able to host an ASP.NET on my home desktop’s Ubuntu box. After researching, I found out that there was an entire open source project pertaining to this exact thing.
I won’t go into much detail about it, but if you are curious search for the ASP.NET Mono Project for more details.
The major issue that I had when setting it up was getting MonoDevelop to install and run. The other issue was that the JavaScript postback when I was trying to host an ASP.NET 4.0 MVC project however when I went to a ASP 3.5 project, everything worked perfectly. I will have to look more into why the ASP.NET 4.0 MVC application wasn’t working, but so far I’m happy with it
.
Title Case
Problem:
Capitalizing the first character of each word in a string (i.e. “the final countdown” → “The Final Countdown”).
Solution:
C#:
C# has a built-in function for this. Its called ‘toTitleCase’, hidden deep within the System.Globalization namespace.
So how do you use it?
using System.Globalization; ... // Get the instance of the TextInfo class to use to (no constructor), comes from the current thread TextInfo info = (System.Threading.Thread.CurrentThread.CurrentCulture).TextInfo; string sample = "hello world"; // Print to console the title case // Outputs: Hello World Console.WriteLine(info.ToTitleCase(sample)); ...
The ‘ToTitleCase’ function returns an instance of a string which will have all of the first characters in words changed to upper case, and leaves the rest of the text as is. This means that if a word is in all capital letters it will remain that way. A simple work around for this is to call the string object’s ‘ToLower’ function before we send the string into the ‘ToTitleCase’ function.
For example,
using System.Globalization; ... // Get the instance of the TextInfo class to use to (no constructor), comes from the current thread TextInfo info = (System.Threading.Thread.CurrentThread.CurrentCulture).TextInfo; string sample = "HELLO world"; // Print to console the title case // Outputs: HELLO World Console.WriteLine(info.ToTitleCase(sample)); // Pre-lowercase everything // Outputs: Hello World Console.WriteLine(info.ToTitleCase(sample.ToLower())); ...
PHP:
The PHP version of this function is a fair bit easier to get to. PHP’s function is called ‘ucwords’. However, similar to the C# version you should always have the string sent in in lower case if you want it to only make the first character of each word upper case (it only changes the first character and doesn’t touch the others).
// Outputs: The Final Countdown echo ucwords ( 'the final countdown' );