Monday, April 21, 2008

Installing Windows Service from Command Prompt

.NET framework provided “installutil. In the folder where the executable for your service application resides, run this utility to install or uninstall the application. Following is the syntax for install and uninstall.

installutil myService.exe
installutil /u myService

Using command line to install Windows Service

This usefull command is not the most intuitive command. The sc command allows you to install/delete Windows Services. I have used this nifty command quite often in the past project to remove erroneous Windows Service. Microsoft has a good KB article.

Things to watch out is the escaping of the binPath. If there are spaces in your binPath you must escape it using the " character which must be escaped again using the \ character.

For example: c:\sc create MyService binPath= "\"c:\program files\my service\service.exe\""

Sunday, April 20, 2008

How to Import Music from an iPod?

This is a legal way to do this, using loopholes in the Windows XP configuration.

Here's a way to quickly get files off a PC iPod without any extra software.

  1. Plug in your iPod.
  2. From the Desktop, go to My Computer and find the iPod (usually designated by a drive letter. i e. E: or F:)
  3. Open the folder.
  4. Go to Tools > Folder Options > View (the tab) > scroll to Show Hidden Files and Folders and click it.
  5. Click OK
  6. Go back to your iPod folder/drive, there you will find a new folder called iPod_Control
  7. Open it.
  8. The Music folder in it will have a bunch of folders named F00, F01 and so on. THEY HAVE YOUR MP3s. Look through the folders to find your stuff.

Friday, April 18, 2008

Using Visual Studio .NET Built-in Web Server from Command Line

Ooooh, we love those undocumented utilities! Developers love the new built-in Web server; they'll like it even more at the command line.

Everyone should take time to browse the command line from time-to-time. You never know what you'll find lurking about in the recesses of all those folders on your system. The other day, I was exploring the .NET Framework 2.0 folder, \WINDOWS\Microsoft.NET\Framework\v2.0.50727. I found all of the utilities I expected — and one that I didn't.

It was a new application, named WebDev.WebServer.EXE. Other than the very odd filename, anyone who's worked with Visual Web Developer will instantly understand the implications of that application. This program lets you execute Web applications from within Visual Web Developer and Visual Studio 2005 using the built-in Web server.
I can't stress enough the importance of this particular application. The built-in Web server is one of the best ideas that Microsoft has had in a long time. You can create a local Web site using a folder containing Web files of any type, not just ASP.NET files. For example, I tested it with my personal Web site. It came right up in the browser using the built-in Web server.

While that's kind of cool, you should know the whole story before you decide about this particular .NET Framework 2.0 feature. No one but you can access the built-in Web server. First, it can use odd port numbers instead of the more common port 80. Also, Microsoft didn't design the server for outside access. Consequently, you can run your personal server without fear of prying eyes.

Wednesday, April 16, 2008

Clearing MS VS.NET 2005 Recent Project History

I guess everyone using MS VS.NET 2005 would have noticed the "Start
Page" which contains the history of recent projects opened. The number
for projects opened to be remembered can be configured at
Tools>Options>Environment>General>Recent Files

If a history project's solution or project file was moved/deleted, then
the VS.NET itself would recognize it and would ask the user to if it
needs to remove it from the history list. But what if, I need to remove
a project in the history list even if it is not moved/deleted?

I just found a registry solution.

HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\8.0\ProjectMRUList

The above registry path refers to the location where VS.NET stores the
recently opened projects. You can clear this location to make VS.NET
remember afresh.

Get calling function name from the called function in C#

I was looking to retrieve the function name from where a desired
function is called so that I can use it for logging purpose. I found
that it could be obtained using StackFrame object which contains the
entire hierarchy of the function stack.

private StackFrame GetCallingStackFrame()
{
// determine the method that called the one that is calling this one.
// this is not just two up the stack because of rich exception support.
StackFrame sf = null;

// start at 2. 1 for this one and another for the one above that.
StackTrace st = new StackTrace(2, true);
Type thisType = GetType();
foreach (StackFrame sfi in st.GetFrames())
{
// find a calling method that is not part of this log class but is part of the same namespace
Type callType = sfi.GetMethod().DeclaringType;
if (callType != thisType && callType.Namespace == thisType.Namespace && !callType.IsInterface)
{ sf = sfi; break; }
}
return sf;
}

This can then be used like so.

StackFrame sf = GetCallingStackFrame();
if (sf != null) // if found add info to log message
{
System.Reflection.MethodBase mb = sf.GetMethod();
string methodName = mb != null ? mb.Name : string.Empty;
string fileName = sf.GetFileName();

if (fileName != null)
fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
int lineNumber = sf.GetFileLineNumber();

if (fileName != null)
strMsg = fileName + " (" + lineNumber + ") - " + methodName + "
- " + strMsg;
else
strMsg = "unknown - " + methodName + " - " + strMsg;
}