Random Findings as a Developer

May 17, 2011

Don’t use File when you mean Directory

Filed under: C# — Andrew @ 1:49 pm

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
    // Outputs: false
    // Outputs: true
    // Outputs: true
    // Put the thread to sleep temporarily

May 11, 2011

Objects Remaining in Memory

Filed under: C#,Programming Languages — Andrew @ 5:37 pm

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)
// Call the garbage collector
// Delete the old file

May 4, 2011

Hosting ASP.NET projects on a Linux Server

Filed under: C# — Andrew @ 6:47 pm

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 :).

Powered by WordPress