assembly information
Attributes that previously had to be configured by hand in the AssemblyInfo file contained in the
project can also be set via the Assembly Information button. This information is important, because
it shows up when an application is installed and when the properties of a file are viewed in Windows
Explorer. Figure 6-7 (left) shows the assembly information for a sample application and Figure 6-7
(right) shows the properties of the compiled executable.
fiGure 6-7
Project Properties .
99
Each of the properties set in the Assembly Information dialog is represented by an attribute that
is applied to the assembly. This means that the assembly can be queried in code to retrieve this
information. In Visual Basic, the My.Application.Info namespace provides an easy way to
retrieve this information.
User account Control settings
Visual Studio 2010 provides support for developing applications that work with User
Account Control (UAC) under Windows Vista and Windows 7. This involves generating an
assembly manifest file, which is an XML file that notifies the operating system if an application
requires administrative privileges on startup. In Visual Basic applications, the View Windows
Settings button on the Application tab can be used to generate and add an assembly manifest file
for UAC to your application. The following code shows the default manifest file that is generated by
Visual Studio.
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"
xmlns:asmv1="urn:schemas-microsoft-com:asm.v1"
xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- UAC Manifest Options
If you want to change the Windows User Account Control level replace the
requestedExecutionLevel node with one of the following.
<requestedExecutionLevel level="asInvoker" />
<requestedExecutionLevel level="requireAdministrator" />
<requestedExecutionLevel level="highestAvailable" />
If you want to utilize File and Registry Virtualization for backward
compatibility then delete the requestedExecutionLevel node.
-->
<requestedExecutionLevel level="asInvoker" />
</requestedPrivileges>
<applicationRequestMinimum>
<defaultAssemblyRequest permissionSetReference="Custom" />
<PermissionSet ID="Custom" SameSite="site" />
</applicationRequestMinimum>
</security>
</trustInfo>
</asmv1:assembly>
If the UAC-requested execution level is changed from the default asInvoker to require
Administrator, Windows will present a UAC prompt when the application is launched. If you
have UAC enabled, Visual Studio 2010 will also prompt to restart in administrator mode if an
100
.
chaPter 6 SoluTionS, projecTS, And iTemS
application requiring admin rights is started
in Debug mode. Figure 6-8 shows the prompt
that is shown on Windows allowing you to
restart Visual Studio in administrator mode.
If you agree to the restart, Visual Studio
will not only restart with administrative
privileges, it will also reopen your solution
including all files you had opened. It will even
remember the last cursor position.
application framework (Visual Basic only)
Additional application settings are available for Visual Basic Windows Forms projects because they
can use the Application Framework that is exclusive to Visual Basic. This extends the standard event
model to provide a series of application events and settings that control the behavior of the application.
You can enable the Application Framework by checking the Enable Application Framework checkbox.
The following three checkboxes control the behavior of the Application Framework:
.
Enable XP Visual Styles: XP visual styles are a feature that significantly improves the
look and feel of applications running on Windows XP or later, because it provides a much
smoother interface through the use of rounded buttons and controls that dynamically
change color as the mouse passes over them. Visual Basic applications enable XP styles by
default and can be disabled from the Project Settings dialog, or controlled from within code
through the EnableVisualStyles method on the Application class.
.
Make Single Instance Application: Most applications support multiple instances running
concurrently. However, an application opened more than two or three times may be run
only once, with successive executions simply invoking the original application. Such an
application could be a document editor, whereby successive executions simply open a
different document. This functionality can be easily added by marking the application as a
single instance.
.
Save My.Settings on Shutdown: Selecting the Save My.Settings on Shutdown option
ensures that any changes made to user-scoped settings will be preserved, saving the settings
provided prior to the application shutting down.
This section also allows you to select an authentication mode for the application. By default this is
set to Windows, which uses the currently logged-on user. Selecting Application-defined allows you
to use a custom authentication module.
You can also identify a form to be used as a splash screen when the application is first launched, and
specify the shutdown behavior of the application.
compile (Visual basic only)
The Compile section of the project settings, as shown in Figure 6-9, enables the developer to control
how and where the project is built. For example, the output path can be modified so that it points
to an alternative location. This might be important if the output is to be used elsewhere in the
build process.
fiGure 6-8
Project Properties .
101
fiGure 6-9
The Configuration drop-down selector at the top of the tab page allows different build settings for
the Debug and Release build configuration.
If your dialog is missing the Configuration drop-down selector, you need to check the Show
Advanced Build Configurations property in the Projects and Solutions node of the Options window,
accessible from the Tools menu. Unfortunately, this property is not checked for some of the setting
profiles — for example, the Visual Basic Developer profile.
Some Visual Basic – specific properties can be configured in the Compile pane. Option Explicit
determines whether variables that are used in code have to be explicitly defined. Option Strict forces the
type of variables to be defined, rather than it being late - bound. Option Compare determines whether
strings are compared using binary or text comparison operators. Option Infer specifies whether to
allow local type inference in variable declarations or whether the type must be explicitly stated.
All four of these compile options can be controlled at either the Project or File-
level. File-level compiler options will override the Project-level options.
The Compile pane also defines a number of different compiler options that can be adjusted to
improve the reliability of your code. For example, unused variables may only warrant a warning,
whereas a path that doesn’t return a value is more serious and should generate a build error. It is
possible either to disable all these warnings or treat all of them as errors.
102 .
chaPter 6 SoluTionS, projecTS, And iTemS
Visual Basic developers also have the capability to generate XML documentation. Of course,
because the documentation takes time to generate, it is recommended that you disable this option
for debug builds. This will speed up the debugging cycle; however, when turned off warnings will
not be given for missing XML documentation.
The last element of the Compile pane is the Build Events button. Click this button to view
commands that can be executed prior to and after the build. Because not all builds are successful,
the execution of the post-build event can depend on a successful build. C# projects have a separate
Build Events tab in the project properties pages for configuring pre- and post-build events.
build (c# and f# only)
The Build tab, shown in Figure 6-10, is the C# equivalent of the Visual Basic Compile tab. This
tab enables the developer to specify the project’s build configuration settings. For example, the
Optimize code setting can be enabled, which results in assemblies that are smaller, faster, and
more efficient. However, these optimizations typically increase the build time, and as such are not
recommended for the Debug build.
fiGure 6-10
On the Build tab, the DEBUG and TRACE compilation constants can be enabled. Alternatively, you
can easily define your own constants by specifying them in the Conditional compilation symbols
textbox. The value of these constants can be queried from code at compile-time. For example, the
DEBUG constant can be queried as follows:
Project Properties .
103
c#
#if(DEBUG)
MessageBox.Show("The debug constant is defined");
#endif
Vb
#If DEBUG Then
MessageBox.Show("The debug constant is defined")
#End If
The compilation constants are defined on the Advanced Compiler Settings dialog, which can be
displayed by clicking the Advanced Compile Options... button on the Compile tab.
The Configuration drop-down selector at the top of the tab page allows different build settings for
the Debug and Release build configuration. You can find more information on the Build options in
Chapter 45.
build events (c# and f# only)
The Build Events tab allows you to perform additional actions before or after the build process.
In Figure 6-11, you can see a post-build event that executes the FXCop Static Code Analysis tool
after every successful build.
fiGure 6-11
You can use environment variables such as ProgramFiles in your command lines by enclosing them
with the percent character. A number of macros are also available, such as ProjectPath. These
macros are listed on the Edit Pre-build and Edit Post-build dialog box.
debug
The Debug tab, shown in Figure 6-12, determines how the application will be executed when run
from within Visual Studio 2010. This tab is not visible for web applications — instead the Web tab
is used to configure similar options.
104 .
chaPter 6 SoluTionS, projecTS, And iTemS
fiGure 6-12
start action
When a project is set to start up, this set of radio buttons controls what actually happens when the
application is run. Initially, this is set to start the project, which will call the Startup object specified
on the Application tab. The other options are to either run an executable or launch a specific web site.
start options
The options that can be specified when running an application are additional command-line
arguments (generally used in conjunction with an executable start action) and the initial working
directory. You can also specify to start the application on a remote computer. Of course, this is
possible only when debugging is enabled on a remote machine.
enable Debuggers
Debugging can be extended to include unmanaged code and SQL Server. The Visual Studio
hosting process can also be enabled here. This process has a number of benefits associated with
the performance and functionality of the debugger. The benefits fall into three categories. First, the
hosting process acts as a background host for the application you are debugging. In order to
debug a managed application, various administrative tasks must be performed, such as creating
an AppDomain and associating the debugger, which take time. With the hosting process enabled,
these tasks are handled in the background, resulting in a much quicker load time during debugging.
Second, in Visual Studio 2010, it is quite easy to create, debug, and deploy applications that
run under partial trust. The hosting process is an important tool in this process because it gives
you the ability to run and debug an application in partial trust. Without this process, the
application would run in full trust mode, preventing you from debugging the application in
partial trust mode.
Project Properties .
105
The last benefit that the hosting process provides is design-time evaluation of expressions. This
is in effect an optical illusion, because the hosting process is actually running in the background.
However, using the Immediate window as you’re writing your code means that you can easily
evaluate expressions, call methods, and even hit breakpoints without running up the entire
application.
references (Visual basic only)
The References tab enables the developer to reference classes in other .NET assemblies, projects,
and native DLLs. Once the project or DLL has been added to the references list, a class can be
accessed either by its full name, including namespace, or the namespace can be imported into a code
file so the class can be referenced by just the class name. Figure 6-13 shows the References tab for a
project that has a reference to a number of framework assemblies.
fiGure 6-13
One of the features of this tab for Visual Basic developers is the Unused References button, which
performs a search to determine which references can be removed. It is also possible to add a
reference path, which will include all assemblies in that location.
Once an assembly has been added to the reference list, any public class contained within that
assembly can be referenced within the project. Where a class is embedded in a namespace (which
might be a nested hierarchy), referencing a class requires the full class name. Both Visual Basic and
106 .
chaPter 6 SoluTionS, projecTS, And iTemS