Archive for December, 2007
Don’t forget to close StreamWriter
In the midst of all my testing, learning, and blogging, my employer occasionally expects me to write a little code. As a result, I’ve spent most of this week being a productive employee, so you’ll have to wait a little while longer for the LINQ articles. In the meantime, I wanted to share a bone head problem I ran into while working on this new project.
The new project is fairly straight forward: read some data from a database, analyze it, look for inconsistencies, correct the inconsistencies, update the database, log the changes in a text file. OK, there’s more going on, but this is the general gist of things.
Having completed the difficult bits, namely recognizing and correcting “user input irregularities”, I moved on to the seemingly mindless task of writing the before and after information to a log file. How many times have we all done this:
foreach (var item in collection)
{
file.WriteLine("Some Text");
}
Simple, right? I ran a test and opened the text file and all appeared to be well. A few tests later, while viewing the results in Excel 2007, I noticed that the last line was cut off. Surely the problem could not be with Excel, I thought, so I looked at the raw text file, and the last line was cut off so many characters in from the left.
Naturally, all sorts of questions start flying through my head. Can an open StreamWriter only handle so many lines? Or could it be a limitation on number of bytes? Is there some system or program memory setting I’m not aware of? The code was so simple that it had to be something more…
But alas, a little digging quickly led me to the answer: it was in fact my code. I failed to Close the StreamWriter, and so there were bytes that even though I had issued WriteLine(), had not yet been saved (or flushed) to the Text file.
I added Close() and sure enough all my data was written to the text file.
foreach (var item in collection)
{
file.WriteLine("Some Text");
}
// Close the StreamWriter to flush all the data to the file!
file.Close();
It’s good once in a while to run into something like this: it keeps us humble. Like the Roman Generals of old it reminds us that “we are mortal”.
Cheers, and Merry Christmas.
4 commentsVista and Special Folders
Way back in January I posted about using Windows Special Folders for storing application data. Today, while setting up a new Vista PC for my boss, I came across a great reason why this approach is a good idea.
I was installing one of our applications that stores a serialized file in the special folder LocalApplicationData. My boss has a ton of configuration information in his current install of this application, and I thought it would be a good thing to simply copy his current configuration over to his new machine. Hey, anything that makes his life easier makes my life easier!
So I began by copying his binary file to a network location for later retrieval. I then went to his new Vista PC … and couldn’t find the correct folder path. I looked all through the Vista folder structure, which only served to frustrate me. On XP, the directory path was something like C:\Documents and Settings\%user%\Local Applications\Application Data\. In Vista C:\Documents and Settings\%user%\ has been replaced with simply C:\Users\%user%\.
But I couldn’t find anything there like what I needed, nor anywhere else for that matter, so I wrote a quick little Console application to output the Special Folder path. Sure enough, it turns out to be C:\Users\%user%\AppData\Local. I went back to Windows Explorer and could not see the folder. Of course, that is when I realized that the folder was hidden.
No problem, I thought, I just need to alter the view to show hidden files and folders, something I’ve done a hundred times on Windows machines. So I right-click in the folder, select “Customize This Folder”, and … nothing. No option to show hidden items. Hmmm, they must have moved it. I right-click and try “Properties”. Still no joy.
Finally, in the tool bar, under the “Organize” drop down, I find an option for “Folder and Search Options”. This window has a “View” tab that reveals the old View options I am used to seeing. Finally! I checked the option for “Show hidden files and folders” (and made a couple of other changes while I was in there) and now I can finally find the correct path. I hate relearning old tricks.
So back to the lesson: I found the correct path, which had been created by my application, retrieved the binary file from the Network store, and replaced the new one created by the application install. I open the application and Viola! – all my boss’s configurations are happily restored.
So, take advantage of the Special Folder system. The folder structures between XP and Vista are quite different, but using this approach made reinstalling my old .NET 1.1 application a happy affair.
No commentsDeveloping For .NET is a year old!
Well, it’s official: today marks the first anniversary of the first post here at developingfor.net. One year ago today, a friend and former co-worker Jim Burnett wrote the first post about ini File Access with VB.NET. Jim only wrote a few other posts before moving on to other projects, but he got the ball rolling.
Over the last year, there have been 53 posts, almost 7,000 unique visitors, and over 11,000 page views. The average view numbers steadily climb every month. Several of the posts are even frequently submitted to Digg. What this tells me is that some readers out there like the site and are getting something out of it. That was always the goal: to help others.
What I would like is to hear from you, the reader. I’d like to know what you like, what you don’t like, and what you’d like to see. Send requests for topics, samples, or code that you would like to see here on the site.
So thanks for making 2007 a great start: register today and post a comment, help me make this site even better in 2008.
– Joel Cochran
No commentsNew Vista Machine follow up
I let VS2008 finish installing last night while I was at home. I got in this morning, and the system had done an automatic update. It loaded a ton of updates from Microsoft, some were Vista specific but most were not. I was able to confirm that VS2008 installed just fine and I have spent the rest of the morning installing other software.
My experience today has been much different than yesterday. Everything seems to be sailing along: I’ve installed a number of applications, configured some network connections, a printer, installed FireFox and extensions, and did some other configuration. All in all I have nothing to complain about: it seems the Windows Update did a lot of good for this machine.
I’ll probably spend the rest of the day setting up some other goodies, and at lunch time I am picking up my new 22″ Widescreen LCD monitor. After that, I have some SPROC development to do. I promise I’ll be getting to LINQ soon.
No commentsNew ThinkPad T61 with Vista Business
My new laptop arrived today. I’ve been playing with it for a few hours now and thought I would share my initial reactions with you.
This is a Core Duo 2.2Ghz processor with 3GB of RAM. It has a 15.4″ widescreen LCD display, which is gorgeous. My favorite feature so far is the integrated fingerprint reader: logging in is super easy and secure. But software installation is shocklingly slow. It only took an hour or so to get the initial Vista install configured, but everything after that has been less than optimum.
I was so excited by my new machine that I went to Staples at lunch time and picked up a new Logitech wireless mouse and keyboard. I bought a docking station with this laptop for the office, one goal of which is to switch back to a regular mouse, keyboard, and monitor. The keyboard part is largely to restore my hands, which hurt quite a bit after working day in and day out on a laptop keyboard. Of course, I know I could have used a regular keyboard with my old laptop, but I found that it pushed the screen to far away. With the new display, that is not an issue. I’m also hoping to use both the built in LCD and an external 22″ widescreen monitor, which should give me loads of space for Visual Studio and related tools. Anyway, I connected my new mouse and keyboard (I haven’t bought the monitor yet) via the USB connector and they worked immediately. I did not even need to push the device “Connect” buttons.
After that, it was time to start installing software. First, I tried to install IBM’s iSeries Access for Windows, V5R4. This was the first task for the CD drive, and it sounded like a rocket ship taking off. It got about half way through the install, whirring, buzzing, and scraping all the while, and then it just quit. I tried to bring up Task Maanger, but it would not show itself. Finally, I popped the disc out and reset it. I don’t think I had it seated all the way although it seemed fine. I resumed the setup and the noises were gone. At the end of setup, I received a “Updating software, please wait” message that never went away. Eventually, I succeeded in bringing up the Task Manager and the process showed running but 0% CPU. I ended it, thinking the install failed, but it appears to have worked.
I closed Task Manager, but in my shortcut tray there were now 2 task manager icons that would not go away, and clicking on either did not reactivate the Task Manager. Sigh. I decided a reboot was in order.
Except the machine would not reboot. I tried restart, log off, and shut down. I tried them from the Start menu and from Task Manager. Each responded to the mouse click yet accomplished nothing. Finally, I did the old push and hold the power button technique to get the machine to shut off. When I brought it back up, I received no errors or warnings about a previous incorrect shutdown.
While it was coming back up, I hit the Google Highway and found that this is a fairly typical problem: the USB devices connected to the machine were preventing it from shutting down. Now, the only two devices connected are my printer and the Mouse and Keyboard connector mentioned above. According to the complaints I found on the web, the Mouse was most likely the culprit and the solution was to update the drivers. Now, I’ve used Logitech accessories for about 7 years, and I’ve never been a fan of their software. I just want a stinking mouse and keyboard that work, I don’t need to use them to program my dishwasher. The last few times I’ve installed their products I have been able to forego installing their software, but this time I figured I’d better do it if I want to be able to shut down my machine, so I did.
The good news is that installing “Logitech SetPoint 3.3a” seems to have solved the problem. After the install I was able to restart from the Start menu just fine. The bad news was that it took almost 15 minutes to install.
I have a stack of other software to install, including of course our erstwhile Visual Studio 2008 and Expression Blend Preview 2. I’ll let you know how these go as well.
Update:
Xara Xtreme, an awesome (and inexpensive) vector based graphics tool installed no problem and took less than 5 minutes.
New problem: I can’t get to the Network. It appears that Norton Internet Security, which came preloaded and is active on this machine, is preventing me from seeing my network. I’m investigating this problem now, but I can’t get on with the installs until I can get to the network path where they are stored.
Update 2:
OK, annoying, but I basically had to disable the Norton Personal Firewall, at which point the network location I was seeking became visible. I established a connection and the system offered to remember the password, which I said yes to. Then, the Fingerprint reader asked me to verify who I was with a swipe of a digit. Way cool! Anyway, it’s off to VS2008 next.
Update 3:
New problem: I am using Microsoft’s Virtual CDRom Control Panel to mount the VS2008 ISO files to a local drive. When I execute the program, it denies access to the needed files. I found in Vista you can right-click and Run the application as Administrator, which worked. I was able to install the driver and mount the file to a local drive letter. The problem now is that the Computer window will not show or find the mounted drive letter. I guess I’ll either need a different emulator or to try and find a Vista update for Virtual CDRom.
Update 4:
OK, I found a freeware utility called Virtual CloneDrive that appears to work on Vista. I was able to install it and mount the ISO file to a drive. Immediately, the Autorun.exe started (which never worked on Virtual CDRom XP for me) and I began installing VS2008. It is 3.5 GB (after I removed C++ and Crystal Reports), so it might be a while. I’m going to let it run and head home. I’ll check in tomorrow.
3 comments
