How to add a Uninstall option in Visual Studio Setup project without writing code

Using Visual Studio 2005/2008, you don’t need to write any code to add a uninstall option for a Setup project (Yes I know some people can write code to do it)

1) In the Setup Project –> File System windows –> Right Click “File System on Target machine” –> add a Special Folder, select System Folder;

2) Into this system folder Add a file. Browse for msiexec.exe from local System32 folder and add it. Override default properties of this file as follows:

Condition:=Not Installed (make sure you put ‘Not Installed’ exactly like that, same case and everything),
Permanent:=True,
System:=True,
Transitive:=True,
Vital:=False.

3) Create a new shortcut under the ‘Users Program Menu’, Set Target to the System Folder which you created in the step 1. and point it’s at the msiexec.exe. Rename the shortcut to ‘Uninstall Your Application’. Set the Arguments property to /x{space}[ProductCode].

5) Build the project, ignore warning about the fact that msiexec should be excluded, DONT exclude it or the setup project wont build.

The ‘Not Installed’ condition and Permananet:=True ensure that the msiexec.exe is only placed into the system folder as part of the install IF it doesn’t aready exist, and it is not removed on an uninstall – therefore it;s pretty safe to ignore that warning and just go for it.

(Based on the description from SlapHead)

Create SQL Server Database using C#

Here is a sample from codeproject :

private void CreateDatabase(DatabaseParam DBParam)
{
    System.Data.SqlClient.SqlConnection tmpConn;
    string sqlCreateDBQuery;
    tmpConn = new SqlConnection();
    tmpConn.ConnectionString = "SERVER = " + DBParam.ServerName +
                         "; DATABASE = master; User ID = sa; Pwd = sa";
    sqlCreateDBQuery = " CREATE DATABASE "
                       + DBParam.DatabaseName
                       + " ON PRIMARY "
                       + " (NAME = " + DBParam.DataFileName +", "
                       + " FILENAME = '" + DBParam.DataPathName +"', "
                       + " SIZE = 2MB,"
                       + " FILEGROWTH =" + DBParam.DataFileGrowth +") "
                       + " LOG ON (NAME =" + DBParam.LogFileName +", "
                       + " FILENAME = '" + DBParam.LogPathName + "', "
                       + " SIZE = 1MB, "
                       + " FILEGROWTH =" + DBParam.LogFileGrowth +") ";
     SqlCommand myCommand = new SqlCommand(sqlCreateDBQuery, tmpConn);
     try
     {
         tmpConn.Open();
         MessageBox.Show(sqlCreateDBQuery);
         myCommand.ExecuteNonQuery();
         MessageBox.Show("Database has been created successfully!",
                           "Create Database", MessageBoxButtons.OK,
                                       MessageBoxIcon.Information);
      }
     catch (System.Exception ex)
     {
         MessageBox.Show(ex.ToString(), "Create Database",
                                     MessageBoxButtons.OK,
                              MessageBoxIcon.Information);
     }
     finally
     {
         tmpConn.Close();
     }
     return;
}

There are many others. Another sample from Microsoft :

using System;
using System.Data.SqlClient;

String str;
    SqlConnection myConn = new SqlConnection ("Server=localhost;Integrated security=SSPI;database=master");

    str = "CREATE DATABASE MyDatabase ON PRIMARY " +
        "(NAME = MyDatabase_Data, " +
        "FILENAME = 'C:\\MyDatabaseData.mdf', " +
        "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
        "LOG ON (NAME = MyDatabase_Log, " +
        "FILENAME = 'C:\\MyDatabaseLog.ldf', " +
        "SIZE = 1MB, " +
        "MAXSIZE = 5MB, " +
        "FILEGROWTH = 10%)";

    SqlCommand myCommand = new SqlCommand(str, myConn);
    try
    {
        myConn.Open();
    myCommand.ExecuteNonQuery();
    MessageBox.Show("DataBase is Created Successfully", "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    catch (System.Exception ex)
    {
    MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    finally
    {
    if (myConn.State == ConnectionState.Open)
    {
        myConn.Close();
    }
    }

If you want to create a database that is similar to the SQL Server Model database, and you want the database in the default location, then change the str variable in the code, as in the following sample code:

str = "CREATE DATABASE MyDatabase"

Also, the 3rd sample is using Microsoft “SQLDMO.dll”:

(you can find the SQLDMO.dll in C:\Program Files\Microsoft SQL Server\80\Tools\Binn or download it from http://www.driverskit.com/dll/sqldmo.dll/3449.html
to add refference to a project , From project menu select “add refference” -> select the browse tab and select the dll file from the location and add to the project
)

public bool CreateDb(string serverName, string Uid, string Pwd, string dbName)
        {
            bool result = true;
            SQLDMO.SQLServer gSQLServerDMO = new SQLDMO.SQLServer();
            SQLDMO.Database nDatabase = new SQLDMO.Database();
            SQLDMO.DBFile nDBFileData = new SQLDMO.DBFile();
            SQLDMO.LogFile nLogFile = new SQLDMO.LogFile();

         
            try
            {
                gSQLServerDMO.LoginSecure = true;
                gSQLServerDMO.Connect(serverName, Uid, Pwd);

                nDatabase.Name = dbName;

                nDBFileData.Name = dbName;

                nDBFileData.PhysicalName = gSQLServerDMO.Registry.SQLDataRoot + "\\DATA\\" + dbName + "_Data.mdf";
              
                nDBFileData.PrimaryFile = true;

                nDBFileData.Size = 2;

                nDBFileData.FileGrowthType = SQLDMO.SQLDMO_GROWTH_TYPE.SQLDMOGrowth_MB;

                nDBFileData.FileGrowth = 1;

                //Add the DBFile object

                nDatabase.FileGroups.Item("PRIMARY").DBFiles.Add(nDBFileData);
               //

                nLogFile.Name = dbName + "Log";

                nLogFile.PhysicalName = gSQLServerDMO.Registry.SQLDataRoot + "\\DATA\\" + dbName + "_Log.ldf";
               
                nLogFile.Size = 2;

                nDatabase.TransactionLog.LogFiles.Add(nLogFile);

                gSQLServerDMO.Databases.Add(nDatabase);

                MessageBox.Show("Database Created Sucessfully);

                gSQLServerDMO.DisConnect();

            }

            catch (Exception SQLDBException)
            {

                MessageBox.Show(SQLDBException.Message);

                result = false;

            }

            finally
            {

            }
            return (result);
        }

              
Above 3rd sample from here

List enum item names using C#

Assume you have define an enum like :

public enum LanguageCollection : int
{
    English = 0,
    简体中文,
    Español,
    Tuurkish,
    Français
}

Is it possible to populate all enum item to a dropdownlist control without hard code each enum name ? The answer is Yes.

Here is sample code :

this.menuCBLanguage.Items.Clear();
for (int i = 0; i < Enum.GetNames(typeof(LanguageCollection)).Length; i++)
{
     this.menuCBLanguage.Items.Add(Enum.GetName(typeof(LanguageCollection), i));
}

Remove subversion (SVN) source control from a project

How to remove subversion (SVN) source control from a project ?

Too easy ways:

Way 1: Just simply delete all .svn folders under your project folder and all sub folders in Windows Explorer;

Way 2: Create a new folder  which you can get a Non-SVN control project code later, go to your SVN controled project, right click on root folder, TortoiseSVN -> Export , then select the folder that you created just now, click OK, then you will get a clear project without SVN control;

Good luck !

Should I add the Visual Studio .suo and .user files to source control ?

.suo and .user file are two types of hidden user files in Visual Studio project.

.suo file which is a binary file. .user file is the project user file which is a text file.

You don’t need to add these 2 files to your source control (SCM) since they contain per-user settings and SUO (Solution User Options) records all of the options that you might associate with your solution so that each time you open it, it includes customizations that you have made.

What is .suo file in Visual Studio project ?

Copied explanation from Microsoft official site:

Solution Files (.sln and .suo)

Visual Studio uses two file types (.sln and .suo) to store settings specific to solutions. These files, known collectively as solution files, provide Solution Explorer with the information it needs to display a graphical interface for managing your files. They allow you to concentrate on your projects and final goals rather than on the environment itself each time you return to your development tasks.

.sln:   Visual Studio Solution

Organizes projects, project items and solution items into the solution by providing the environment with references to their locations on disk.

.suo  Solution User Options

Records all of the options that you might associate with your solution so that each time you open it, it includes customizations that you have made.

Content from here

New site after missing my old CodeEase site

The old CodeEase site was created in 2008 or 2009, but there was a serious accident , or you can say it was my fault: I forgot to renew the hosting and did not have a time to check the status, until everything gone, finally the entire site gone, I even did not backup my site !

Very bad and sad ! I almost wanted to give up, I did not want to build a new site anymore !

Until this week, I felt that I should continue the code writing and collections, I need to record all beautiful code, Because I can not live without code. That is it.

So, let me, and my partners, or even you guys, build a completely new CodeEase.com ! Welcome ! And share your ideas, and, most important is: code.