Monday, November 21, 2011

Shape Catcher–Unicode character recognition

Came across ShapeCatcher, a useful site that allows you to draw a shape and it suggests different unicode characters that match that shape. Definitely beats looking through Window’s CharacterMap looking for that font that looks like a clock.

image

 

Or searching for the peace symbol:

image

Friday, November 18, 2011

Setting up FTP under Windows Server 2008 R2 with user-isolation

In Windows Server 2008 R2. (Important these are not steps for a production environment. Just for setting up a test FTP server.)
In Server Manager:
Under Roles, click Add Role
    install FTP
        Under roles - add Web Server (IIS)
            Under role services select FTP Server
        Complete the installation
In IIS: Create a FTP Site:  
    Right click on Sites and select Add FTP Site
image
        Name it: Whatever you like
        Path: C:\inetpub\ftproot
image
    Bindings and SSL Settings
        Select No SSL
image

    Authentication information
        Select Basic authentication
            Select Allow access to "All users" with Read and Write permissions
image
    Finish the FTP creation wizard
   
Under settings for the FTP site (in IIS)
    Select FTP User Isolation
        Select "User name directory (disable global virtual directories)"
image
image
   
Accounts:
Create local user accounts (this is how users get access to the FTP site (or at least one way how you can provide access to your ftp site)). (example: create a local account named FtpUser1)
Create a group called FTPUsers and add user accounts created above to the group. (it makes it easier to provide access to folders as shown in the next step).
Provide the group you created in the above step (FTPUsers) modify priviledges on C:\inetpub\ftproot
Next create a sub-folder called LocalUser (C:\inetpub\ftproot\LocalUser). (important to name it exactly that).  
Create a folders for each of the user accounts you created above (eg: if you created an account call FtpUser1, create a folder under LocalUser called FtpUser1).

Test:
Test access to your FTP site via your favorite FTP client. If you did everything correctly, you should have a valid FTP site with an account setup.

FTP 7.5 on Windows Server 2008 R2

If you search for enabling FTP on Windows Server 2008, you most often get redirected to the following site:

http://www.iis.net/download/FTP

And if you download that FTP7.5 installer referenced on that page and try and run it, you get the following error:

image

(this version of the operating system is not supported. FTP Service for IIS 7.0 can be installed only on Windows Server 2008).

Turns out that, for Windows Server 2008 R2, you don’t need to download a separate installer. You just need to enable the FTP role. (This is because on Windows Server 2008 R2, you get IIS 7.5 and not IIS 7.0).

More info: http://learn.iis.net/page.aspx/263/installing-and-configuring-ftp-7-on-iis-7/

Wednesday, November 16, 2011

Powershell–Run script via context menu

I wanted the ability to right click on a PS1 file and then execute a powershell script. (A “run powershell script” from Context-Menu option)

image

So here is how you can do it:

Copy the following text into a text file and save it:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\Shell\Run with powershell]

[HKEY_CLASSES_ROOT\*\Shell\Run with powershell\command]
@="C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit &'%1'"

Save the file and rename it to have an extension “.reg”. Now double click the file and you will have a context menu called “Run with powershell”

To make life easier you can download my version of the file from:

https://docs.google.com/open?id=0BzvtUeIvT94wMzg1MWRjZjEtZTUyNi00YTlkLWJhMDUtYjczMTljYzdmNmYy

Monday, November 14, 2011

SqlBulkCopy error: given value cannot be converted to type xxxx

If you get the following error when using SqlBulkCopy: “The given value of type String from the data source cannot be converted to type int of the specified target column.”

Then check for 2 things:

Make sure that you are not setting more characters than what is allowed for a field (i.e., column allows only 5 characters and you set the column to 6 characters).

If you are not setting all the columns, then make sure you use the “SqlBulkCopyColumnMapping”, to setup the mapping (especially true, when you are inserting rows and you are not setting the primary key value as its setup to be an identity column).

Thursday, November 03, 2011

EntLib–Logging–SwitchValues

Here are the list of values that you can use for the SwitchValue for the configuration of the logging block in Enterprise Library.

Value

Description

ActivityTracing

Allows the Stop, Start, Suspend, Transfer, and Resume events through.

All

Allows all events through.

Critical

Allows only Critical events through. A critical event is a fatal error or application crash.

Error

Allows Critical and Error events through. An Error event is a recoverable error.

Information

Allows Critical, Error, Warning, and Information events through. An information event is an informational message.

Off

Does not allow any events through.

Verbose

Allows Critical, Error, Warning, Information, and Verbose events through. A Verbose event is a debugging trace.

Warning

Allows Critical, Error, and Warning events through. A Warning event can indicate both critical and non-critical issues.

MSDN: http://msdn.microsoft.com/en-us/library/ff664426(v=PandP.50).aspx

Wednesday, November 02, 2011

C#–Loading dlls at runtime and finding classes that implement an interface

Given: An interface that you have defined:

public interface IMyPluginInterface
{
    int GetInteger();
}

When: Your application runs

Then: It should load dlls at run time and call GetInteger on all those classes that implement the interface IMyPluginInterface

Solution: Here is how you can do that:

static void Main(string[] args)
{
    //find some dlls at runtime
    string[] dlls = Directory.GetFiles(Environment.CurrentDirectory, "MyPlugin*.dll");

    //loop through the found dlls and load them
    foreach (string dll in dlls)
    {
        System.Reflection.Assembly plugin = System.Reflection.Assembly.LoadFile(dll);
       
        //now find the classes that implement the interface IMyPluginInterface and get an object of that type
        var instances = from t in plugin.GetTypes()
                        where t.GetInterfaces().Contains(typeof(IMyPluginInterface))
                                 && t.GetConstructor(Type.EmptyTypes) != null
                        select Activator.CreateInstance(t) as IMyPluginInterface;

        //now call the GetInteger method defined by the interface
        foreach (var instance in instances)
        {
            Console.WriteLine(instance.GetInteger());
        }

    }

    Console.ReadLine();
}

public class MyPlugin1 : IMyPluginInterface
{
    public int GetInteger()
    {
        return 1;
    }
}