Understanding Project Files
Each Visual Basic project is composed of several code files. Some of them are by default visible to the developer and are the ones that you need to edit to create your application. There are also some other files (which are hidden by default but that can be made visible manually) that we can consider as support files. To understand what kind of support these files offer, we have to think that most of the settings that we can provide to our applications via the My Project window are represented with Visual Basic code. Particularly, Visual Basic translates into code the content of the Application, Resources, Settings and My Extensions tabs. In this chapter you get a detailed description of files that represent the Application tab in My Project and then you get an overview of files that represent other tabs. Although you seldom edit these code files manually, because all of them have design time support from My Project (as detailed in Chapter 20, "The 'My' Namespace," when describing the My namespace), there could be some situations in which you need to manually edit them, so it's important to know something about them. Before going on, you need to click the Show All Files button in Solution Explorer. This gives visibility to several code files that are hidden by default and that provide the main infrastructure for each Visual Basic project.
Dissecting My Project
In Chapter 2 we introduced the My Project window and saw how it offers graphical tools for specifying some settings when developing applications, such as application information and compile options. Understanding My Project is important because it also provides the infrastructure of the My namespace, offering the ability for specifying important settings that are discussed in Chapter 20. For now, we need to know that My Project offers a graphical representation of information that is stored in some code files. In Solution Explorer you can notice an element named My Project. When you double-click this element, you are redirected to the My Project window. But when you enable the All Files view, you notice how the My Project element becomes a folder that can be expanded. Within this folder (which is physically stored inside the project's folder and contains all files described in this section), you can notice the presence of several files packaged into the assembly's metadata when you build the project. We now describe such files and how they work.
The Application.myapp file is an XML representation of the project's main properties. Listing 3.1 shows the content of this file as it becomes available when you create a new Console application.
Listing 3.1. The Content of Application.myapp
<?xml version="1.0" encoding="utf-8"?> <MyApplicationData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <MySubMain>false</MySubMain> <SingleInstance>false</SingleInstance> <ShutdownMode>0</ShutdownMode> <EnableVisualStyles>true</EnableVisualStyles> <AuthenticationMode>0</AuthenticationMode> <ApplicationType>2</ApplicationType> <SaveMySettingsOnExit>true</SaveMySettingsOnExit> </MyApplicationData>
XML elements in this file are self-explanatory, and you may notice how each of them represents a particular item on the Application tab. The Application.myapp file is the brother of another file named Application.Designer.vb. Such a file basically stores information related to Windows Forms applications such as the authentication mode and the shutdown mode. It is the complement for those application options that you can see in the Windows Application Framework Properties group in the Application tab. Listing 3.2 shows the content of the Application.Designer.vb as it is generated for a Windows Forms application.
Listing 3.2. The Content of Application.Designer.vb
Partial Friend Class MyApplication Public Sub New() MyBase.New(Global.Microsoft.VisualBasic.ApplicationServices. AuthenticationMode.Windows) Me.IsSingleInstance = false Me.EnableVisualStyles = true Me.SaveMySettingsOnExit = true Me.ShutDownStyle = Global.Microsoft.VisualBasic.ApplicationServices. ShutdownMode.AfterMainFormCloses End Sub Protected Overrides Sub OnCreateMainForm() Me.MainForm = Global.WindowsApplication1.Form1 End Sub End Class
For the sake of simplicity, in the preceding code some attributes are omitted that Visual Studio adds to class members and that are related to the debugger interaction. As you can see examining Listing 3.2, items in the Application tab of My Project have been mapped to Visual Basic properties. The Me identifier represents the instance of the current application. The OnCreateMainForm method establishes which window must be the startup one. In this case Form1 is the default name that Visual Studio assigns to the main window when a new project is created. If you also examine the code inside the IDE, you can notice how there are some comments in the code that advise that the code itself is auto-generated and that you should not edit it manually, because you can use the My Project designer that will automatically map changes to the Visual Basic code. You might need to set custom actions for application events (such as Startup or Shutdown, which are usually handled in the ApplicationEvents.vb file) and Application.designer.vb is the right place.
In Chapter 2 we discussed the Assembly Information dialog, describing how it is used for specifying information about applications. All the information is stored in a file named AssemblyInfo.vb. Listing 3.3 shows the content of this file as it is available when you create a new project.
Listing 3.3. AssemblyInfo.vb Content
Imports System Imports System.Reflection Imports System.Runtime.InteropServices ' General Information about an assembly is controlled through the following ' set of attributes. Change these attribute values to modify the information ' associated with an assembly. ' Review the values of the assembly attributes <Assembly: AssemblyTitle("WindowsApplication1")> <Assembly: AssemblyDescription("")> <Assembly: AssemblyCompany("")> <Assembly: AssemblyProduct("WindowsApplication1")> <Assembly: AssemblyCopyright("Copyright © 2009")> <Assembly: AssemblyTrademark("")> <Assembly: ComVisible(False)> 'The following GUID is for the ID of the typelib if this project is exposed to COM <Assembly: Guid("5572d199-a7ca-48c3-98d3-56533cd6ba86")> ' Version information for an assembly consists of the following four values: ' ' Major Version ' Minor Version ' Build Number ' Revision ' ' You can specify all the values or you can default the Build and Revision Numbers ' by using the '*' as shown below: ' <Assembly: AssemblyVersion("1.0.*")> <Assembly: AssemblyVersion("188.8.131.52")> <Assembly: AssemblyFileVersion("184.108.40.206")>
As you can notice examining the Visual Basic code shown in Listing 3.3, there are several items whose identifier begins with the word Assembly, such as AssemblyTitle, AssemblyCompany, and so on. Each item is in relationship with fields of the Assembly Information dialog. Moreover such items are marked with an attribute named Assembly. Attributes are discussed in Chapter 48. The reason why it is useful knowing about the above file is that there are situations in which you need to edit this file manually. Examples are localization of WPF applications or marking an assembly as compliant to Microsoft Common Language Specifications.
Resources and the Resources.resx File
Visual Studio 2010 enables defining resources that you can embed in your assembly's metadata and use within your applications. Resources can include strings, icons, picture files, audio files, and so on. My Project offers a tab named Resources that provides a visual way for defining project level resources.
Figure 3.1 shows the Resources designer with the definition of a String resource named TextMessage that has a value and a description. We revisit the Resources tab in Chapter 20, where we discuss My namespace, but if you are curious, you can play with the designer to see what kind of resources you can add.
Figure 3.1 The My Resources designer.
Resources are supported by two files stored inside the My Project folder: Resources.resx and Resources.designer.vb. The first one is basically an XML schema used by Visual Studio for working with resources. Listing 3.4 shows the content of the schema.
Listing 3.4. The Content of Resources.resx
<root> <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xsd:import namespace="http://www.w3.org/XML/1998/namespace" /> <xsd:element name="root" msdata:IsDataSet="true"> <xsd:complexType> <xsd:choice maxOccurs="unbounded"> <xsd:element name="metadata"> <xsd:complexType> <xsd:sequence> <xsd:element name="value" type="xsd:string" minOccurs="0" /> </xsd:sequence> <xsd:attribute name="name" use="required" type="xsd:string" /> <xsd:attribute name="type" type="xsd:string" /> <xsd:attribute name="mimetype" type="xsd:string" /> <xsd:attribute ref="xml:space" /> </xsd:complexType> </xsd:element> <xsd:element name="assembly"> <xsd:complexType> <xsd:attribute name="alias" type="xsd:string" /> <xsd:attribute name="name" type="xsd:string" /> </xsd:complexType> </xsd:element> <xsd:element name="data"> <xsd:complexType> <xsd:sequence> <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" /> </xsd:sequence> <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" /> <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" /> <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" /> <xsd:attribute ref="xml:space" /> </xsd:complexType> </xsd:element> <xsd:element name="resheader"> <xsd:complexType> <xsd:sequence> <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> </xsd:sequence> <xsd:attribute name="name" type="xsd:string" use="required" /> </xsd:complexType> </xsd:element> </xsd:choice> </xsd:complexType> </xsd:element> </xsd:schema> <resheader name="resmimetype"> <value>text/microsoft-resx</value> </resheader> <resheader name="version"> <value>2.0</value> </resheader> <resheader name="reader"> <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=220.127.116.11, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> </resheader> <resheader name="writer"> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Ver- sion=18.104.22.168, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> </resheader> <data name="TextMessage" xml:space="preserve"> <value>Hello Visual Basic 2010!</value> <comment>This is a test string</comment> </data> </root>
This schema establishes how a resource is defined, with names, values, comments, and a MIME type that identifies the file type. At the end of the XML markup code, you can see how resources are stored. Continuing with our example, you can see the name of the resource (inside the data element), its value, and the description we provided via the designer. This schema is used by Visual Studio for design time purposes. To work with resources in our applications, Visual Studio also needs to provide Visual Basic code support for resources. Such support is provided by a code file named Resources.designer.vb. This file handles a reference to a .NET object called ResourceManager that is responsible for managing resources in code. Listing 3.5 shows the content of Resources.designer.vb. (For the sake of simplicity, auto-generated attributes are not covered here.)
Listing 3.5. Content of Resources.designer.vb
Friend Module Resources Private resourceMan As Global.System.Resources.ResourceManager Private resourceCulture As Global.System.Globalization.CultureInfo Friend ReadOnly Property ResourceManager() As Global.System.Resources.ResourceManager Get If Object.ReferenceEquals(resourceMan, Nothing) Then Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("MyFirst2010Program.Resources", GetType(Resources).Assembly) resourceMan = temp End If Return resourceMan End Get End Property Friend Property Culture() As Global.System.Globalization.CultureInfo Get Return resourceCulture End Get Set(ByVal value As Global.System.Globalization.CultureInfo) resourceCulture = value End Set End Property Friend ReadOnly Property TextMessage() As String Get Return ResourceManager.GetString("TextMessage", resourceCulture) End Get End Property End Module
At this point in the book you don't effectively need to know what each type used in code refers to, whereas it is useful to know the existence of the ResourceManager property that points to the project resources. (See the declaration of the temp variable.) This handles a reference to the application-level ResourceManager that enables access to resources. There is another property named Culture that is of type System.Globalization.CultureInfo. This property sets or returns the current localization for resources. The last property in the code is named TextMessage and is the Visual Basic representation of the string resource defined in My Project. This is a read-only property, because you cannot change it in code (you can change it only via designer) and returns a localized version of the resource invoking the GetString method of the ResourceManager class. GetString requires an object of type CultureInfo (in our code it's resourceCulture) that represents the culture that the resource must be localized to. The following line of code shows how you can access the preceding defined resource, which is discussed further in Chapter 20:
Dim myString As String = My.Resources.TextMessage
When you access resources, as shown in Chapter 20, you do not need to manually invoke this background code, but you need to know how it is structured to better understand what's happening behind the scenes. Resources are not the only feature in My Project that is supported by Visual Basic code for design time features. Settings are another one of these features.
Settings in Visual Basic development are particular objects that provide a managed way for manipulating applications and user level settings. For example, you could provide users with the ability of customizing options in the user interface of your application. To save and read such customizations to and from disk, you can use .NET Settings. My Project provides a tab named Settings that enables specifying information at the application or user level. Figure 3.2 shows an example.
Figure 3.2 Settings Tab in My Project.
As you can see in Figure 3.2, you can specify an identifier for each setting, a type (which you can understand better by reading Chapter 4), the scope, and the value. For the scope, User means that only the user that runs the application can use that setting. Application means that the setting is available at the application level, independently from the user that logged into Windows (and therefore is available to all users). As with Resources, Settings will be also described in detail in Chapter 20. Settings are represented by a simple XML file, named Settings.settings. Listing 3.6 shows the content of Settings.settings after the addition of the sample setting.
Listing 3.6. Settings.settings Content
<?xml version='1.0' encoding='utf-8'?> <SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="My" GeneratedClass- Name="MySettings" UseMySettingsClassName="true"> <Profiles /> <Settings> <Setting Name="StartState" Type="System.Boolean" Scope="User"> <Value Profile="(Default)">True</Value> </Setting> </Settings> </SettingsFile>
In the XML markup you can see the presence of a Settings node that stores as many Setting elements and as many settings that you specify in My Project. In our example there is just one Setting element that contains the name of the setting, the data type, the scope, and the default value (which means the value you specify in My Project). The Settings.settings file also has Visual Basic support, which is represented by another file named Settings.designer.vb. We do not need to examine all the content of this file when just a couple of parts of the code can be interesting for us. First, this file implements a property named Settings that is accessible via the My namespace, as detailed in Chapter 20. Listing 3.7 shows the definition of this property.
Listing 3.7. Definition of the Settings Property
Friend ReadOnly Property Settings() As Global.MyFirst2010Program.My.MySettings Get Return Global.MyFirst2010Program.My.MySettings.Default End Get End Property
The Settings property represents the active instance of the Settings object that you can use in your applications. How the active instance is defined is beyond the scope of this chapter, but now you know that the Settings tab in My Project also has a counterpart in two support files. Just for your convenience, the following line of code shows how you can access settings and how we set them before:
Dim currentValue As Boolean = My.Settings.StartState
The value stored in the StartState setting will be assigned to a variable named currentValue. Examining My namespace in Chapter 20 can clarify the usage of Settings and Resources and of many other interesting features.