Add if conditional statement to the Visual Studio pre-build and post-build events

There are times when you want to execute statements in the pre-build and post-build events of a Microsoft Visual Studio project when certain conditions met. In the next example we will copy the environment specific config file to the web application folder if the configuration is set to “Local”.

To edit the pre-buld and post-buld events

  • In the Visual Studio Solution Explorer right click the startup project
  • Select Properties
  • On the left side select the Build Events tab
  • To edit the script in a larger, resizable  window click the Edit Pre-Build or Edit Post-Build buttons

 

visual studio edit pre-build event

 

  • Enter the following lines into the event edit region:


if $(ConfigurationName) ==  Local (
attrib "$(ProjectDir)Web.config" -r
copy "$(ProjectDir)Config_2012\$(ConfigurationName).xml" "$(ProjectDir)Web.config"
)

The commands will be interpreted line-by-line the same way as a DOS batch file, so it is important to place the opening parenthesis “(” in the same line as the if statement.

How to add the Config folder to a Microsoft Visual Studio project even if the folder is not in the project directory

Corporations maintain multiple server environments for development, testing and production purposes.
All of these environments require specific settings to connect to the databases and other resources. When the build master builds the application for the specific environment the build process has to select the appropriate configuration and build it into the installer file for deployment.
It is a good practice to store these environment specific config files in a folder that also designates the Visual Studio version.

Console applications and Windows services

For console applications and Windows services place this folder into the “Source” folder.

visual studio add folder to project folder tree

 

In Microsoft Visual Studio you can add a folder to a project, but it will be created in the directory where the project file is located. To add a folder to a project when the folder is not in the project directory we have to manually edit the project file. In this example we will add a folder to our main project that contains the environment specific config files. The “Config_2012” folder is located in the “Source” folder outside of the “Project_2012” project directory.

The following steps will add the “Config_2012” folder to the project

  • Create the “Config_2012” folder in Windows Explorer
  • Add the environment specific config files to it
  • Add the folder to source control
  • Open the solution in Visual Studio
  • Right click the main project and select Unload Project
  • Right click the main project again and select Edit…
  • Add the following to the project file

<ItemGroup>
<None Include="..\Source\Config_2012\Dev.config">
<Link>Config_2012\Dev.config</Link>
</None>
<None Include="..\Source\Config_2012\Local.config">
<Link>Config_2012\Local.config</Link>
</None>
<None Include="..\Source\Config_2012\Prod.config">
<Link>Config_2012\Prod.config</Link>
</None>
<None Include="..\Source\Config_2012\QA.config">
<Link>Config_2012\QA.config</Link>
</None>
<None Include="..\Source\Config_2012\Staging.config">
<Link>Config_2012\Staging.config</Link>
</None>
<None Include="..\Source\Config_2012\UAT.config">
<Link>Config_2012\UAT.config</Link>
</None>
</ItemGroup>

 

  • Save the project file
  • Right click the main project file and select Reload Project

The “Config_2012” folder will appear in the Solution Explorer and the config files will be added as linked files.

visual studio add folder to project

Web applications and web services

For web applications and web services place the Config folder into the web application folder

web application folder structure config_2012 folder highlighted

To add the config folder and files to the web application

  • Open the web solution in Visual Studio
  • Select the web project
  • Click the Show All Files button on the Solution Explorer toolbar

visual studio show all files button

 

  • The Solution Explorer will display all files in the web application folder

web application solution explorer show all files config_2012 folder and files

 

  • Right click the Config_2012 folder  and select Include In Project

web application solution explorer show all files config_2012 folder include in project

 

  • Select all the config files in the Config_2012 folder
  • Right click the selection and select Properties

web application solution explorer show all files config_2012 folder files properties

 

  • Set the Build Action to None

web application file properties build action none

 

  • Click the Show All Files button on the Solution Explorer toolbar again to hide the excluded files

visual studio show all files button

 

Create a backup of a Microsoft SQL Server database to a location outside of the database server

The Microsoft SQL Server Management Studio 2008 user interface only allows you to create a backup of a database to one of the local drives of the database server.

To create a backup of a Microsoft SQL Server database to a location outside of the database server execute the following SQL script:


BACKUP DATABASE MyDatabase TO
DISK = N'\\MyNas\MyDatabase.bak'
WITH NOFORMAT, NOINIT, NAME = N'MyDatabase Database Backup', NOSKIP, STATS = 10
GO

Change the bold values to match your environment.

How to add sample programming code to a post or page in WordPress

Double quotes

When you write a post or create a page you may want to add sample program snippets to it. If your code contains double quotes (“) the WordPress page will display different characters for the opening and closing quotes, like in this   “example”.

Unfortunately these characters differ from the quotes you use for programming, so if someone pastes the sample code into a code editor the compiler will not understand them.

To force WordPress to display your lines as those were intended

  • Paste the sample code into the article in Visual mode
  • Switch to Text mode and place <code> and </code> tags around the code snippet

 HTML or XML

When you insert HTML or XML code into you page make sure you keep the order described above. The visual editor will convert the < and > characters to &lt; and &gt;, so the WordPress engine will not interpret your sample code as part of the HTML of the page.

If you enter the following in Text mode

<code>
<html>Text</html>
</code>

WordPress will remove your < and > characters and the page will display

Text

Save the list of folders in a text file

If you receive a portable drive and want to see the list of folders, you can output it to a text file.

To save the list of folders in a text file execute the following command in the command window:


dir P: /AD /B >> "C:\folderlist.txt"

where P: is the drive letter of the portable drive

The file will be created in the root directory of the C: drive.

How to remove Windows 8 and install Windows 7 on a Lenovo ideapad z580 laptop

My friend just bought a new Lenovo ideapad z580 laptop with Windows 8 on it. She was not sure if her special statistical programs will run on Windows 8, so she asked me to remove Windows 8 and install her copy of Windows 7  64 bit on it. As it turned out the Lenovo laptop arrived with UEFI boot turned on and it did not accept the original Windows 7 64 bit DVD as a boot device. The solution is to turn off UEFI in the BIOS and enable the legacy boot.

To enter the BIOS

  • Turn off the Lenovo ideapad z580 laptop
  • Press the small “Novo” button next to the power button to turn on the computer and display the Novo Button Menu
  • In the Novo Button Menu select BIOS Setup and press Enter

Turn off the UEFI boot mode in the BIOS

  • In the BIOS setup select Boot on the top of the screen
  • Press Enter on the Boot Mode option and select Legacy Support
  • Select Boot Priority, press enter and select Legacy First

Save the BIOS settings

  • Select Exit on the top of the screen
  • Select Exit Saving Changes and press Enter
  • Press Enter on Yes to save the changes

The computer will automatically restart into Windows 8

  • Keep the power button pressed to shut it down

Boot from the Windows 7 DVD

  • Start the computer with the “Novo” button
  • Insert the Windows 7 DVD into the DVD drive
  • In the Novo Button Menu select Boot Menu and press Enter
  • In the Boot menu select 2. ATAPI CD… and press Enter

Install Windows 7

  • The computer will boot from the Windows 7 DVD
  • Select Install, Advanced installation
  • Delete all partitions on the hard drive
  • Create one partition (Windows will create another 100MB system partition for itself)
  • Select the large partition as the destination of Windows 7

How to enable jQuery intellisense in Microsoft Visual Studio

To enable jQuery intellisense in Microsoft Visual Studio

  • Open the _references.js file in the editor by double clicking it
  • Drag the jquery-x.x.x.intellisense.js file into the _references.js file in the editor. The name of the file will be added to the _references.js file

Conditional compilation symbols in Microsoft Visual Studio

If you need to make breaking changes in your source code that will be deployed later to production, but first have to be tested in the development environment you can include conditional compilation symbols to specify which lines will be included in the application for certain environments. This way you can maintain environment specific versions of the source code, the current version for  production and the new version for development and testing.

In this example we will make a change in our source code that we want to deploy to the Development environment for testing, but not to QA, UAT and Production.

Create the conditional compilation symbol in the project

  • Right click the project
  • Select Properties
  • On the left side select the Build tab
  • Select Dev in the Configuration dropdown
  • Enter new_version into the Conditional compilation symbols field

visual studio conditional compilation symbol in project

 

Add the conditional compilation symbol to the source code

  • Surround the old and new code with a #if, #else, #endif structure

#if new_version

// New code
int a = 1;

#else

// Old code
int a = 0;

#endif

int b = a;

If the Dev configuration is selected the new code will be compiled into the application

visual studio conditional compilation symbol true

 

If another configuration (QA) is selected the old code will be compiled into the application

visual studio conditional compilation symbol false

Microsoft Visual Studio editor settings

There are some settings that can make your work easier in Visual Studio.

Start Microsoft Visual Studio, and in the “Tools” menu select “Options”

– To insert new tabs to the right of existing tabs and keep the already opened tabs in place and
– Show pinned tabs in a separate row

  • On the left side select “Environment”, “Tabs and Windows”
  • Check “Insert new tabs to the right of existing tabs” and “Show pinned tabs in a separate row”

visual studio options tabs

 

– To use tabs instead of spaces in edited files

  • On the left side select “Text Editor”, “All Languages”, “Tabs”
  • Select “Smart” and “Keep tabs”

visual studio options tab settings

 

Unable to open a setup project in Microsoft Visual Studio 2010 on Windows 7 64 bit

I work on multiple computers using different operating systems.
When I tried to open a web service solution in Microsoft Visual Studio 2010 on my Windows 7 64 bit workstation I got the following error message:

“The project file … has been moved, renamed or is not on your computer.”

visual studio 2010 unable to open project long path

I could verify that the file was on my computer at the stated location, and I could open the same solution in Microsoft Visual Studio 2010 on a Windows Server 2003 32 bit machine. I have recreated the setup file on my Windows 7 64 bit machine, and everything looked all right, but when I closed the solution and tried to open it again I got the same error message. I have counted the length of the full path of the setup project and I got 153. As it turned out, the path was not too long.

The following steps solved the problem, but I still don’t know why:

  • Removed the setup project from the solution
  • Renamed the setup project file by changing one character of the file name (not the extension)
  • Re-added the setup project as an existing project