Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Professional Visual Studio 2005 (2006) [eng]

.pdf
Скачиваний:
117
Добавлен:
16.08.2013
Размер:
21.9 Mб
Скачать

Chapter 36

Along the top of each results window is a small toolbar (shown in Figure 36-8) for navigation within the results themselves. From left to right, these buttons perform the following commands:

Go to location

Go to next location

Stop Search

Clear all

Go to previous location

Figure 36-8

Go To Location: Opens the file containing the current match, and locates the cursor at the specific match

Go to Previous Location: Navigates to the previous match’s location

Go to Next Location: Navigates to the next match’s location

Clear All: Clears the results window

Stop Search: Stops an incremental search

These commands are also accessible through a context menu (see Figure 36-9). Right-click on the particular match you want to look at and choose the Go To Location command. Alternatively, double-clicking on a specific match will also perform this function.

Figure 36-9

Replace in Files

Although it’s useful to search a large number of files and find a number of matches to your search criteria, even better is the Replace in Files action. Accessed via the keyboard shortcut Ctrl+Shift+H, or by clicking the drop-down arrow next to Quick Replace, Replace in Files performs in much the same way as Find in Files, with all the same options.

The main difference is that you can enable an additional Results option when you’re replacing files. When performing a mass replacement action like this, it can be handy to have a final confirmation before committing the changes that occurred. To have this sanity check available to you, enable the :Keep modified files open after Replace All: checkbox (shown at the bottom of Figure 36-10).

486

Find and Replace

Figure 36-10

Note that this feature works only when using Replace All; if you just click Replace, Visual Studio will open the file containing the next match and leave the file open in the IDE anyway.

Important: If you leave this option unchecked and perform a mass replacement on a large number of files, they will be changed permanently without any recourse for an undo action. Be very sure that you know what you’re doing.

Whether you have this option checked or not, after performing a Replace All action, Visual Studio will report back to you how many occurrences were changed (see Figure 36-11). If you’re familiar with the process and don’t care how many, you can uncheck the “Always show this message” option for future searches. However, be aware that you don’t get any other visual feedback indicating when a mass replacement has finished processing, so you may want to leave this dialog window displayed.

Figure 36-11

487

Chapter 36

Incremental Search

If you’re looking for something in the current code window and don’t want to bring up a dialog, the Incremental Search function might be what you need. Invoked by either the Edit Advanced Incremental Search menu command (see Figure 36-12), or using the keyboard shortcut Alt+I, Incremental Search locates the next match based on what you type.

Immediately after invoking Incremental Search, simply begin typing the text you need to find. The mouse pointer will change to a set of binoculars and a down arrow. As you type each character, the editor will move to the next match. For example, typing f would find the first word containing an f — for example, offer. Typing an o would then move the cursor to the first word containing fo — for example, form, and so on.

Using this feature is an incredibly efficient way of navigating through long code blocks when you want to quickly locate the next place you need to work.

Figure 36-12

488

Find and Replace

Find Symbol

In addition to these already comprehensive find and replace tools, there is an additional search feature in Visual Studio 2005. You can now search for symbols that are objects, classes, and procedural names. The Find Symbol dialog is invoked by the keyboard shortcut Alt+F12, or through the menu command Edit Find Symbol. Alternatively, you can switch the normal Find and Replace dialog over to Find Symbol by clicking the drop-down arrow next to Quick Find or Find in Files.

The Find Symbol dialog (see Figure 36-13) has slightly different options from the other Find actions. Rather than scope based on a current document or solution like Quick Find, or the file system like Find in Files, Find Symbol can search through your whole solution, a full component list, or even the entire .NET Framework. In addition, you can include any references added to the solution as part of the scope. To create your own set of components in which to search, click the ellipses next to the Look In field, and browse through and select the .NET and COM components registered in the system, or browse to files or projects.

The Find options are also simplified. You can only search for whole words, substrings (the default option), or a prefix.

After clicking Find All, the search results are compiled and presented in a special tool window entitled Find Symbol Results. By default, this window shares space with the Find Results windows at the bottom of the IDE, and displays each result with any references to the particular object or component. This is extremely handy when you’re trying to determine where and how a particular object is used or referenced from within your project.

Figure 36-13

Find and Replace Options

Believe it or not, you can further customize the find and replace functionality with its own set of options in the main Options dialog. Found in the Environment group, the Find and Replace options (see Figure 36-14) enable you to reset informational and warning message settings as well as indicate whether the Find What field should be automatically filled with the current selection in the editor window.

489

Chapter 36

The final option in the list of checkboxes is inactive by default to allow you to perform subsequent Quick Find or Quick Replace actions, but if you often use Quick Find to find one instance and then want to be able to edit or view that instance, you may want to check this option to hide the dialog after it navigates to the first match.

Figure 36-14

Summar y

As you’ve seen in this chapter, Visual Studio 2005 comes with an excellent set of search and replacement functionality that makes your job a lot easier, even if you need to search entire computer file systems for regular expressions. The additional features such as Find Symbol and Incremental Search also add to your toolset, simplifying the location of code and objects as well.

490

Ser ver Explorer

In Chapter 35 you learned about the numerous windows available in Visual Studio 2005. One of these, the Server Explorer, exposes hardware and services for you to query and work with in your application. This window is divided into two main sections: Servers, which is covered in this chapter, and Data Connections, which is covered in the next chapter.

The Server Explorer, shown in Figure 37-1, has two top-level nodes. The first, Data Connections, enables you to work with all aspects of data connections, including the capability to create databases, add and modify tables, build relationships, and even execute queries. Chapter 38 goes into more detail about setting up and working with data connections.

Figure 37-1

Chapter 37

The Ser vers Node

The Servers node would be better named Computers, as it can be used to attach to and interrogate any computer to which you have access. Under each node (for example, the NICKIBM node) is a list of the hardware, services, and other components that belong to that computer. Each of these nodes contains a number of activities or tasks that can be performed. Some software vendors have components that plug into the Server Explorer.

By default, the local computer appears in the Servers list. To add computers, right-click the Servers node and select Add Server from the context menu. This opens the Add Server dialog shown in Figure 37-2.

Figure 37-2

Entering a computer name or IP address will initiate an attempt to connect to the machine using your credentials. If you do not have sufficient privileges, you can elect to connect using a different user name by clicking on the appropriate link. The link appears to be disabled, but clicking it does bring up a dialog in which you can provide an alternative user name and password.

Event Logs

The Event Logs node gives you access to the machine event logs. You can launch the Event Viewer from the right-click context menu. Alternatively, as shown in Figure 37-3, you can drill into the list of event logs to view the events for a particular application. Clicking on any of the events displays the full information about the event in the Properties window. You can also clear each of the event logs by selecting Clear Event Log from the right-click context menu.

Figure 37-3

492

Server Explorer

Although the Server Explorer is useful for interrogating a machine while writing your code, the true power comes with the component creation you get when you drag particular nodes onto a Windows form. For example, in this case, if you drag the Application node onto a Windows form, you get an instance of the System.Diagnostic.EventLog class added to the nonvisual area of the designer. You can then write an entry to this event log using the following code:

Private Sub ButtonClick(ByVal sender As Object, ByVal e As EventArgs) _

Handles Button1.Click

Me.EventLog1.Source = My.Settings.EventLogName Me.EventLog1.WriteEntry(“Button Clicked”, EventLogEntryType.Information)

End Sub

You can also write exception information using the WriteException method, which accepts an exception and a string that may provide additional debugging information. Unfortunately, you still have to manually set the Source property before calling the WriteEntry method. Of course, this could have been set using the Properties window for the EventLog1 component, but doing so would make it hardcoded into the application. Instead, this is provided as a setting in the configuration file.

An alternative to adding an EventLog class to your code is to use the built-in logging provided by the My namespace. For example, you can modify the previous code snippet to write a log entry using the

Application.Log property:

Private Sub ButtonClick(ByVal sender As Object, ByVal e As EventArgs) _

Handles Button1.Click

My.Application.Log.WriteEntry(“Button Clicked”, EventLogEntryType.Information)

End Sub

This is equivalent to the previous code, even to the extent of having the event source specified in the configuration file. Using the My namespace to write logging information also has a number of additional benefits. In the following configuration file, an EventLogTraceListener is specified to route log information to the event log. However, you can specify other trace listeners — for example, the FileLog TraceListener, which writes information to a log file by adding it to the SharedListeners and Listeners collections:

<?xml version=”1.0” encoding=”utf-8” ?> <configuration>

<system.diagnostics>

<sources>

<source name=”DefaultSource” switchName=”DefaultSwitch”> <listeners>

<add name=”EventLog”/> </listeners>

</source>

</sources>

<switches>

<add name=”DefaultSwitch” value=”Information” /> </switches>

<sharedListeners>

<add name=”EventLog” type=”System.Diagnostics.EventLogTraceListener” initializeData=”ApplicationEventLog”/>

</sharedListeners>

</system.diagnostics>

</configuration>

493

Chapter 37

This configuration also specifies a switch called DefaultSwitch. This switch is associated with the trace information source via the switchName attribute and defines the minimum event type that will be sent to the listed listeners. For example, if the value of this switch were Critical, then events with the type Information would not be written to the event log. The possible values of this switch are as follows:

DefaultSwitch

Event Types Written to Log

 

 

Off

No events

Critical

Critical events

Error

Critical and Error events

Warning

Critical, Error, and Warning events

Information

Critical, Error, Warning, and Information events

Verbose

Critical, Error, Warning, Information, and Verbose

 

events

ActivityTracing

Start, Stop, Suspend, Resume, and Transfer events

All

All events

 

 

Note that there are overloads for both WriteEntry and WriteException that do not require an event type to be specified. These methods will default to Information and Error.

Management Classes

Figure 37-4 shows the full list of management classes available via the Server Explorer. Each node exposes a set of functionality specific to that device or application. For example, right-clicking the Printers node enables you to add a new printer connection, whereas right-clicking the named node under My Computer enables you to add the computer to a domain or workgroup. The one thing common to all these nodes is that they provide a strongly typed wrapper around the Windows Management Instrumentation (WMI) infrastructure. In most cases, it is simply a matter of dragging the node representing the information you are interested in across to the form. From your code you can then access and manipulate that information.

To give you an idea of how these wrappers can be used, this section walks through how you can use the management classes to retrieve information about a user. Start by dragging a user node onto a form. Under the System Accounts node, you can drill down into Authenticated Users Groups Users Users and Accounts. In this node you should see a list of users. Selecting a node and dragging it onto the form will give you a UserAccount component in the nonvisual area of the form, as shown in Figure 37-5.

If you look in the Solution Explorer, you will see that it has also added a custom component called root.CIMV2.Win32_UserAccount.vb (or similar depending on the computer configuration). This custom component is generated by the Management Strongly Typed Class Generator (Mgmtclassgen.exe) and includes the UserAccount and other classes, which will enable you to expose WMI information.

494

Server Explorer

Figure 37-4

Figure 37-5

If you click on the UserAccount1 object on the form, you can see the information about that user in the Properties window. In this application, however, you’re not that interested in that particular user; that user was selected as a template to create the UserAccount class. The UserAccount1 object can be deleted, but before deleting it, copy the Path property of the object, as you need to use this information, combined with the user name entered in the form in Figure 37-5, to load the information about that user. Add the following code to handle the button click event for the Load User Information button:

Public Class MainForm

Private Const CUserPath As String = _

“\\NICKIBM\root\CIMV2:Win32_UserAccount.Domain=””NICKIBM””,Name=””{0}”””

Private Sub BtnRetriveCounters_Click(ByVal sender As System.Object, _

495