Content types for Response object

Written by stevey on August 24th, 2010

I always wondered where to find all the MIME types and subtypes that you usually set in Response.ContentType property? Yes you can Google it. But I found out today that Windows registry has a registry entry under the HKEY_CLASSES_ROOT just for that. This is where you get a complete list of all legal MIME type/subtypes:

HKEY_CLASSES_ROOT\MIME\Database\Content Type

There are five content types Text, Image, Audio, Video and Application. It is the subtypes that have made the MIME\Database\Content Type very large

Examples found under this registry key: application/msword, application/ms-powerpoint, audio/mpeg, application/pdf, audio/x-wav, application/vnd.ms-excel (for exporting web data to Excel), etc.

Under the MIME\Database, you can also find other goodies such as all the Codepage listing.

 

Using Google-hosted jQuery library

Written by stevey on August 9th, 2010

Nice thing about using Google hosted jQuery is that I could call jQuery lib from anywhere and take advantage of versioning  and compression maintenance work done by Google. According to some source, the better caching provided by Google actually speed up the loading of the javascripts.

For example, below is a dialog box that uses jQuery UI hosted at Google. Click on the link to see it in action.

Show Dialog

Source code:

<link href=”http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css” rel=”stylesheet” type=”text/css” />
<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js”></script>
<script src=”http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js”></script>
<script type=”text/javascript”>
function showDialog() {

$(“#dialog”).dialog({autoOpen:false, modal:true,buttons: {OK:function(){$(this).dialog(‘close’); }}});

$(“#dialog”).dialog(‘open’);

}

</script>

<div id=”dialog” title=”hello dialog”></div>

 

Install a Window Service

Written by stevey on August 1st, 2010

Here were the steps I went through to install a Window service called “Yangsoft Greeting” that will simply say a greeting when user first logs in to a PC:

  1. Create a Windows service project using the service template from VS2010
  2. Right click on the service.cs designer surface and add a service installer (ProjectInstaller.cs)
  3. Once I added the ProjectInstaller, there were two installer components needed to be configured properly, one is for service installer and one for service process installer. ServiceInstaller is where you define the name, description, and display name of the service; ServiceProcessInstaller is where you define which account will be used to run the service. The account to choose is LocalService, LocalSystem, networkService and User. The User account requires a user name and password when the service is installed.
  4. After compiling the solution, next step is to install the service so that it will show up in the Windows service list. There are two ways to install a service, one is manually running InstallUtil.exe from command line, for example : C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe    F:\Development\GreetingWinSvc\VoiceGreeting\bin\Release\Yangsoft.VoiceGreeting.exe
  5. The other way is to create a Setup and deployment project and package everything into a MSI file.

 

 

Display Windows Message Box from Console App

Written by stevey on July 27th, 2010

In a Console app, you normally write out message to screen using Console.WriteLine(). How should I display a Windows message box like we usually do in a Windows Form app?

Call the MessageBox method from user32.dll COM API is one of the methods I learned, and here were how it was done (no need to set reference to user32.dll; just do DllImport):

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Runtime.InteropServices;

 

namespace ComInterop

{

class Message

{

 

public static void ShowMessage(string msg,string caption)

{

 

//MessageBox(new IntPtr(0), msg, caption, 0); //type=0 only show OK button

MsgBox(new IntPtr(0), msg, caption, 1); //Type=1 shows both OK and Cancel button

 

 

}

[DllImport(“user32.dll”)]

private static extern int MessageBox(IntPtr hwnd, String text, String caption, uint type);

//if want to use different method name other than what’s given in the type lib, use EntryPoint attribute

[DllImport(“user32.dll”, EntryPoint = “MessageBox”)]

private static extern int MsgBox(IntPtr hwnd, String text, String caption, uint type);

}

 

class Program

{

static void Main(string[] args)

{

//user32.dll DLLImport

Message.ShowMessage(“This method used DllImport”, “DllImport is Cool”);

}

}

 

}

 

 

Playing with sapi.dll

Written by stevey on July 27th, 2010

With Microsoft’s Speech Library, it is extremely easy to add voice to your .Net application.

I created a small Console app in VS2010 and here were the steps I took:

  1. Add reference to sapi.dll, which was found in my computer’s C:\Program Files\Common Files\Microsoft Shared\Speech (Windows XP SP2)
  2. Import the Speech type library namespace
  3. Instantiated a SpVoice and called the Speak method; that was all.
Sample codes:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpeechLib;
namespace ComInterop
{
class Speech
{
public static void SayThis(string words)
{
SpVoice voice = new SpVoice();
voice.Speak(words, SpeechVoiceSpeakFlags.SVSFDefault);
}
}
static void Main(string[] args)
{
////speech library
Console.WriteLine(“Type a few words to speak”);
string words = Console.ReadLine();
Speech.SayThis(words);
Console.ReadKey();
}
}

 

 

Directory Access Control

Written by stevey on July 25th, 2010

The other day I was playing with ACL and got myself locked out of a directory that I tried to access. Then I tried to add a new FileSystemAccessRule with FullControl right and Allow type, hoping to regain the access to the folder; but it turned out that was not the way to do it. I had to call RemoveAccessRule from the DirectorySecurity object in order to re-grant myself access to the folder. So I figured I would make a note here so that myself and others can use as reference when things about ACL get murky again in the future.

To grant a Windows user access to a specific directory, use the following codes (need using these namespaces : System.Security.Principal,System.Security.AccessControl,System.IO)

 

public static void GrantDirectoryAccess(string dir, string userName)

{

DirectorySecurity ds = Directory.GetAccessControl(dir);

ds.AddAccessRule(new FileSystemAccessRule(userName,FileSystemRights.FullControl, AccessControlType.Allow));

Directory.SetAccessControl(dir,ds);

 

}

To deny user Read access to the folder, use the following:

public static void DenyDirectoryAccess(string dir, string username)

{

DirectorySecurity ds = Directory.GetAccessControl(dir);

ds.AddAccessRule(new FileSystemAccessRule(username,FileSystemRights.Read, AccessControlType.Deny));

Directory.SetAccessControl(dir,ds);

}

 

If you want to give user back the access right, you would think that by running GrantDirectoryAccess() method again you can achieve that, correct? Wrong! The ACL rule is if there are Deny and Allow access types both tied to a user or a user group, then the Deny will take precedence.

So, in order to give the access right back, you’ll need to remove the access rule by running the codes below instead:

public static void RemoveDirectoryDeny(string dir, string userName)

{

DirectorySecurity ds = Directory.GetAccessControl(dir);

ds.RemoveAccessRule(new FileSystemAccessRule(userName, FileSystemRights.Read, AccessControlType.Deny));

Directory.SetAccessControl(dir, ds);

}

Here is the sample of how to grant user “steve” access to “c:\test” folder, then deny it, thn re-grant it:

static void Main(string[] args)

{

Console.WriteLine(“Messing around with C:\test”);

//first grant fullcontrol access

GrantDirectoryAccess(@”c:\test”,”steve);

//then deny the Read access

DenyDirectoryAccess(@”c:\test”, “Steve”);

//Tried to run GrantDirectoryAccess() again and see if it worked;, it didn’t

//Had to remove the Deny rule

UserSecurity.RemoveDirectoryDeny(@”c:\test”, “Steve”);

Console.WriteLine(“Access to c:\test has been regained. Press any key to exit”);

Console.ReadKey(true);

 

}