Mastering Microsoft Visual Basic 2008 phần 6 pot

115 272 0
Mastering Microsoft Visual Basic 2008 phần 6 pot

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Petroutsos c14.tex V2 - 01/28/2008 2:35pm Page 540 Petroutsos c15.tex V2 - 01/28/2008 2:59pm Page 541 Chapter 15 Accessing Folders and Files Files have always been an important aspect of programming. We use files to store data, and in many cases we have to manipulate files and folders from within applications. I need not give examples: Just about any application that allows user input must store its data to a file (or multiple files) for later retrieval — databases excluded, of course. Manipulating files and folders is quite common, too. Organizing files into folders and process- ing files en masse are two typical e xamples. I recently ran into a few web-related tasks that are worth mentioning here. A program for placing watermarks on pictures was the first. A watermark is a graphic that’s placed over an image to indicate its origin. The watermark is transparent, so it doesn’t obscure the image, but it makes the image unusable on any site other t han the original one. You will see how to place a semitransparent graphic on top of an image in Chapter 19, ‘‘Manip- ulating Images and Bitmaps,’’ and with the help of the information in this chapter, you’ll be able to scan a folder that has thousands of image files and to automate the process of watermarking the images. Another example has to do with matching filenames to values stored in a database. Product images are usually named after the product’s ID and stored in separate files. There’s a need for programs to match product IDs to images, to find out whether there’s an image for a specific product in the database, or to simply move the image files around (store the images for different product categories into different folders and so on). In this chapter, you’ll learn how to do the following: ◆ Handle files with the My object ◆ Manipulate folders and files ◆ Save data to a file ◆ Monitor changes in the file system and react to them The IO Namespace and the FileSystem Component To manipulate folders and files, as well as file input/output (I/O) operations, the Framework provides the System.IO namespace. The My object provides My.Computer.FileSystem component, which simplifies the basic file tasks. Obviously, there’s an enormous overlap between the two components. The FileSystem component is a subset of the IO namespace in terms of the functionality it exposes, but it’s considerably simpler to use. The My object was designed to simplify some of the most common tasks for the VB developer and, as you may recall from Chapter 1, ‘‘Getting Started with Visual Basic 2008,’’ it’s a speed-dial into the Framework. You can perform all common file I/O operations with a single line of code. (Okay, sometimes you may need a second line, but you Petroutsos c15.tex V2 - 01/28/2008 2:59pm Page 542 542 CHAPTER 15 ACCESSING FOLDERS AND FILES get the idea.) To access the full power of the Framework’s I/O capabilities, use the IO namespace. There’s nothing you can do with the My object that you can’t do with t he Framework; the opposite isn’t true. The My object was designed to simplify the most common programming tasks, but it’s not a substitute for the Framework. That said, I will start with a brief overview of the My.Computer.FileSystem component and then I’ll discuss the IO namespace, which is the whole enchilada. Old VB developers will use the My object to access the file system, because VB is about productivity and the My object is simpler. The Framework, on the other hand, is the core of Windows programming and you shouldn’t ignore it. Using the My.Computer.FileSystem Component Using the My object, you can write some text to a file via a single statement. The WriteAllText method accepts as arguments a path and the string to be written to the file (as well as a third optional argument that determines whether the text will be appended to the file or will replace the current contents), writes some text to the file (the contents of a TextBox control in the following sample), and then closes the file: My.Computer.FileSystem.WriteAllText(fName, TextBox1.Text, True) If the specified file does not exist, the write method creates it. To write binary data to a file, use the WriteAllBytes method, whose syntax is almost identical, but the second argument is an array of bytes instead of a string. By the way, because My is not a class, you can’t import it to a file and shorten the statements that access its members; you have to fully qualify the member names. You can still use the With statement, as shown here: With My.Computer.FileSystem .WriteAllText(fname, TextBox1.Text, True) End With To read back the data saved with the WriteAllText and WriteAllBytes methods, use the ReadAllText and ReadAllBytes methods, respectively. The ReadAllText method accepts as an argument the path of a file and returns its contents as a string. ReadAllBytes accepts the same argument,butreturnsthefile’scontentsasanarrayofbytes.Thisisallyouneedtoknowin order to save data to disk files between sessions with the My object. The following code segment saves the contents of the TextBox1 control to a user-specified file, clears the control, reads the text from the same file, and populates the TextBox1 control: ’ Set up the SaveFileDialog control SaveFileDialog1.DefaultExt = ”*.txt” SaveFileDialog1.AddExtension = True SaveFileDialog1.FileName = ”” SaveFileDialog1.Filter = ”Text Files|*.txt|All Files|*.*” If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then ’ Use the WriteAllText method to save the text My.Computer.FileSystem.WriteAllText( SaveFileDialog1.FileName, TextBox1.Text, False) Petroutsos c15.tex V2 - 01/28/2008 2:59pm Page 543 USING THE MY.COMPUTER.FILESYSTEM COMPONENT 543 End If ’ Clear the control TextBox1.Clear() ’ Set up the OpenFileDialog control OpenFileDialog1.DefaultExt = ”txt” OpenFileDialog1.Filter = ”Text Files|*.txt|All Files|*.*” OpenFileDialog1.FileName = ”Test File.txt” If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then ’ Use the ReadAllText method to read back the text ’ and display it on the TextBox control TextBox1.Text = My.Computer.FileSystem.ReadAllText( OpenFileDialog1.FileName) End If As you can see, it takes two statements to send the data to the file and read it back. All other statements set up the Open and Save As dialog boxes. Here’s another example of using the FileSystem object. To delete a folder, call the Delete- Directory method of the My.Computer.FileSystem component, which accepts three arguments: the name of the folder to be deleted, a constant that specifies whether the DeleteDirectory method should delete the contents of the specified folder if the folder isn’t empty, and another constant that determines whether the folder will be deleted permanently or moved to the Recycle Bin. This constant is a member of the FileIO.RecycleOption enumeration: DeletePermanently (to remove the file permanently from the file system) and SendToRecycleBin (moves the file to the Recycle Bin). To delete a file, use the DeleteFile method, which has the same syntax. (The first argument is the path of a file, not a folder.) Another interesting member of the FileSystem object is the SpecialDirectories property, which allows you to access the special folders on the target computer (folders such as My Documents, the Desktop, the Program Files folder, and so on). Just enter the name of the SpecialDirectories property followed by a period to see the names of the special folders in the IntelliSense box. To find out the application’s current folder, call the CurrentDirectory method. The RenameDirectory and RenameFile methods allow you to rename folders and files, respec- tively. Both methods accept as arguments the original folder name or filename and the new name, and perform the operation. They do not return a value to indicate whether t he operation was successful, but they throw an exception if the operation fails. The CopyFile and CopyDirectory methods copy a single file and an entire folder, respectively. They accept as arguments the path of the file or folder to be copied, the destination path, and an argument that determines which dialog boxes will be displayed during the copying operation. The value of this argument is a member of the FileIO.UIOption enumeration: AllDialogs (shows the progress dialog box and any error dialog boxes) and OnlyErrorDialogs (shows only error dialog boxes). The following code segment copies a fairly large folder. It’s interesting to see how it displays the usual file copy animation and prompts users every time it can’t copy a folder (because the user doesn’t have adequate privileges or because a file is locked, and so on). Dim dir as String dir = ”C:\Program Files\Microsoft Visual Studio 9.0” Try My.Computer.FileSystem.CopyDirectory( dir, ”E:\Copy of ” & Petroutsos c15.tex V2 - 01/28/2008 2:59pm Page 544 544 CHAPTER 15 ACCESSING FOLDERS AND FILES My.Computer.FileSystem.GetName(dir), Microsoft.VisualBasic.FileIO. UIOption.AllDialogs, FileIO.UICancelOption.ThrowException) Catch ex As Exception MsgBox(ex.Message) End Try Please do change the destination drive (E: in the preceding sample code segment); you may not have an E: drive, or you may overwrite a working installation of Visual Studio 2008. Notice that I used the GetName method of the FileSystem component t o extract the last part of the path and then combine it with the new drive name. The last argument of the CopyDirectory method, which is a member of the UICancelOption enumeration: DoNothing or ThrowException, determines how the method reacts when the user clicks the Cancel button on the copy animation (see Figure 15.1). I used the ThrowException member and embedded t he entire statement in an exception handler. If you click the Cancel button while the folder’s files are being copied, the following message will appear: The operation was canceled. Figure 15.1 Copying a large folder by using the DirectoryCopy method Cancelling a copy operation doesn’t reset the destination folder. You must insert some addi- tional code to remove the files that have been copied to the destination folder, or notify the user that some files have copied already and they’re not automatically removed. To manipulate folders, use the CreateDirectory and DirectoryExists methods, which accept as an argument the path of a folder. To find out whether a specific file exists, call the File- Exists method, passing the file’s path as the argument. To retrieve information about drives, folders, and files, use the GetDriveInfo, GetDirectory- Info,andGetFileInfo methods, respectively. These methods accept as an argument the name of the drive or the path to a folder/file, respectively, and return the relevant information as an object. Drive properties are described with the IO.DriveInfo class, folder properties are described wi th the IO.DirectoryInfo class, and file properties with the IO.File Info class. These objects are part of the Framework’s IO namespace and they provide properties such as a directory’s path and attributes, a file’s path, size, creation and last modification date, and so on. The three objects are described in Petroutsos c15.tex V2 - 01/28/2008 2:59pm Page 545 USING THE MY.COMPUTER.FILESYSTEM COMPONENT 545 detail later in this chapter, in the discussion of the IO namespace. To find out the properties of the C: drive on your system, execute a statement such as the following: Dim DI As IO.DriveInfo = My.Computer.FileSystem.GetDriveInfo(”C”) Debug.WriteLine(”DRIVE ” & DI.Name & vbCrLf & ”VOLUME ” & DI.VolumeLabel & vbCrLf & ”TYPE ” & DI.DriveType.ToString & vbCrLf & ”TOTAL SIZE ” & DI.TotalSize.ToString & vbCrLf & ”FREE SPACE ” & DI.AvailableFreeSpace.ToString) This statement produced the following output on my system: DRIVE C:\ VOLUME VAIO TYPE Fixed TOTAL SIZE 3100019372032 FREE SPACE 50142416896 To retrieve information about all drives in your system, call the Drives method, which returns a read-only collection of DriveInfo objects. If you want to search a folder for specific files, use the FindInFiles method, which is quite flexible. The FindInFiles method goes through all files in a specified folder and selects files by a wildcard specification, or by a string in their contents. The method has two overloaded forms; their syntax is the following: FindInFiles(dir, containsText, ignoreCase, FileIO.SearchOption) and FindInFiles(dir, containsText, ignoreCase, FileIO.SearchOption,fileWildCards() String) Both methods return the list of matching files as a read-only collection of strings. The dir argu- ment is the folder to be searched, and the containsText argument is the string we want to locate in the files. The ignoreCase argument is a True/False value that determines whether the search is case-sensitive, and the SearchOption argument is a member of the FileIO.SearchOption enu- meration and specifies whether the method will search in the specified folder or will include the subfolders as well: SearchAllSubdirectories, SearchTopLevelOnly. The second overloaded form of the method accepts an additional argument, which is an array of strings with the patterns to be matched (for example, *.txt, Sales*.doc, *.xls, and so on). The following statements locate all text, .doc,and.xml files in the Program Files folder that contain the string Visual Basic. The search is case-insensitive and includes the all subfolders under Program Files. Dim patterns() As String = {”*.txt”, ”*.doc”, ”*.xml”} Dim foundFiles As System.Collections.ObjectModel. ReadOnlyCollection(Of String) Petroutsos c15.tex V2 - 01/28/2008 2:59pm Page 546 546 CHAPTER 15 ACCESSING FOLDERS AND FILES foundFiles = My.Computer.FileSystem.FindInFiles( ”C:\Program Files”, ”visual basic”, True, FileIO.SearchOption.SearchAllSubDirectories, patterns) Dim file As String For Each file In foundFiles Debug.WriteLine(file) Next A Simpler Method of Saving Data to Files The Framework provides an attractive alternative to writing data to files: the serialization mechanism. You can create collections of objects and persist them to a file via a few simple statements. Actually, it’s much simpler to create a collection of customer/product/sales data and persist it as a whole, than to write code to write every field to a file (let’s not forget the code for reading the data back into the application). Serialization is a major component of .NET, and it’s discussed in detail in Chapter 16, ‘‘XML and Object Serialization.’’ This concludes the overview of the file-related methods of the FileSystem component. This component doesn’t expose many members, and their syntax is quite simple. You can experiment with the methods and properties of the FileSystem component to get a better idea of the type of operations you can perform with it. In the remainder of this chapter, you’ll find a detailed discussion of the IO namespace. Manipulating Folders and Files with the IO Namespace In this section, you’ll learn how to access and manipulate files and folders with the help of the Directory and File classes of the System.IO namespace. The Directory class provides methods for manipulating folders, and the File class provides methods for manipulating files. These two objects allow you to perform just about any of the usual operations o n folders and files, respectively, short of storing data into or reading from files. By the way, directory is another name for folder;thetwo terms mean the same thing, but folder is the more-familiar term in Windows. When it comes to developers and administrators, Microsoft still uses directory (the Active Directory, the Directory object, and so on), especially with command-line utilities. Keep in mind that Directory and File objects don’t represent folders or files. Directory and File are shared classes, and you must supply the name of the folder or file they will act upon as an argument to the appropriate method. The two classes that represent folders and files are the DirectoryInfo and FileInfo classes. If you’re in doubt about which class you should use in your code, consider that the members of the Directory and File classes are shared: You can call them without having to explicitly create an instance of the corresponding object first, and you must supply the name of the folder or file their methods will act upon as an argument. The methods of the DirectoryInfo and FileInfo classes are instance methods: Their methods apply to the folder or file represented by the current instance of the class. Petroutsos c15.tex V2 - 01/28/2008 2:59pm Page 547 MANIPULATING FOLDERS AND FILES WITH THE IO NAMESPACE 547 Both the Directory and the DirectoryInfo classes allow you to delete a folder, including its subfolders. The Delete method of the DirectoryInfo class will act on a directory you specified when you instantiated the class: Dim DI As New System.IO.DirectoryInfo(”C:\Work Files\Assignments”) DI.Delete() Butyoucan’tcallDelete on a DirectoryInfo object that you haven’t specifically declared. The DirectoryInfo.Delete method doesn’t accept the name of a folder as an argument. The Delete method of the Directory class, on the other hand, deletes the folder passed as an argument to the method: System.IO.Directory.Delete(”C:\Work Files\Assignments”) The Directory Class The System.IO.Directory class exposes all the members you need to manipulate folders. Because the Directory class belongs to the System.IO namespace, you must import the IO namespace into any project that might require the Directory object’s members with the following statement: Imports System.IO Methods The Directory o bject exposes methods for accessing folders and their contents, which are described in the following sections. CreateDirectory This method creates a new folder, whose path is passed to the method as a string argument: Directory.CreateDirectory(path) path is the path of the folder you want to create and can be either an absolute or a relative path. If it’s a relative path, its absolute value is determined by the current drive and path (use the GetCurrentDirectory method to find out the absolute current path). The CreateDirectory method returns a DirectoryInfo object, which contains information about the newly created folder. The DirectoryInfo object is discussed later in this chapter, along with the FileInfo object. Notice that the CreateDirectory method can create multiple nested folders in a single call. The following statement will create the folder folder1 (if it doesn’t exist), folder2 (if it doesn’t exist) under folder1, and finally folder3 under folder2 in the C: drive: Directory.CreateDirectory(”C:\folder1\folder2\folder3”) If folder1 exists already, but it doesn’t contain a subfolder named folder2,thenfolder2 will be automatically created. An exception will be thrown if the total path is too long or if your Petroutsos c15.tex V2 - 01/28/2008 2:59pm Page 548 548 CHAPTER 15 ACCESSING FOLDERS AND FILES application doesn’t have permission to create a folder in the specified path. However, no exception will be thrown if the specified path already exists on the disk. The method will simply not create any new folders. It will still return a DirectoryInfo object, which describes the existing folder. Delete This method deletes a folder and a ll the files in it. If the folder contains subfolders, the Delete method will optionally remove the entire directory tree under the node you’re removing. The simplest form of the Delete method accepts as an argument the path of the folder to be deleted: Directory.Delete(path) This method will delete the specified path only. If the specified folder contains subfolders, they will not be deleted and, therefore, the specified folder won’t be deleted, either. To delete a folder recursively (that is, also delete any subfolders under it), use the following form of the Delete method, which accepts a second argument: Directory.Delete(path, recursive) The recursive argument is a True/False value. Set it to True to delete recursively the subfold- ers under the specified folder. This method deletes folders permanently (it doesn’t send them to the Recycle Bin). The statements in Listing 15.1 attempt to delete a single folder. If the folder contains subfolders, the Delete method will fail, and the structured exception handler will be activated. The e xception handler examines the type of the exception, and if it was caused b ecause the folder isn’t empty, the exception handler prompts the user about whether it should delete the contents of the folder. If the user gives permission to delete the folder’s contents, the code calls the second form of the Delete method, forcing it to delete the folder recursively. Listing 15.1: Deleting a Directory Private Sub bttnDelete Click( ) Handles bttnDelete.Click Directory.CreateDirectory( ”c:/folder1/folder2/folder3”) Try Directory.Delete(”c:\folder1”, False) Catch exc As IOException If exc.Message.IndexOf( ”The directory is not empty”) > -1 Then Dim reply As MsgBoxResult reply = MsgBox( ”Delete all files and subfolders?”, MsgBoxStyle.YesNo, ”Directory Not Empty”) If reply = MsgBoxResult.Yes Then Try Directory.Delete(”c:\folder1”, True) Catch ex As Exception MsgBox(”Failed to delete folder” & vbCrLf & ex.Message) End Try Else MsgBox(exc.Message) Petroutsos c15.tex V2 - 01/28/2008 2:59pm Page 549 MANIPULATING FOLDERS AND FILES WITH THE IO NAMESPACE 549 End If End If End Try End Sub Notice the nested Try Catch statement that catches unauthorized exceptions (you may not have the rights to delete the specific folder). Exists This method accepts a path as an argument and returns a True/False value indicating whether the specified folder exists: Directory.Exists(path) The Delete method will throw an e xception if you attempt to delete a folder that doesn’t exist, so you can use the Exists method to make sure the folder exists before attempting to delete it: If Directory.Exists(path) Then Directory.Delete(path) Move This method moves an entire folder to another location in the file system; its syntax is the fol- lowing, where source is the name of the folder to be moved and destination is the name of the destination folder: Directory.Move(source, destination) The Move method doesn’t work along different volumes, and the destination can’t be the same as the source argument, obviously. Notice the lack of a Copy method that would copy an entire folder to a different location. To copy a folder, you must manually create an identical folder structure and then copy the corre- sponding files to the proper subfolders. The FileSystem component p rovides a MoveFile and a MoveFolder method, which move a single file and an entire folder, respectively. GetCurrentDirectory, SetCurrentDirectory Use these methods to retrieve and set the path of the current directory. The current directory is a basic concept when working with files. This is the folder in which all files specified by name will be saved and where the application will look for files specified by their name, not their complete path. Also, relative paths are resolved according to their relation to the current directory. By default, the GetCurrentDirectory method returns the folder in which the application is running. SetCurrentDirectory accepts a string argument, which is a path, and sets the current directory to the specified path. You can change the current folder by specifying an absolute or a relative path, such as the following: Directory.SetCurrentDirectory(” \Resources”) The two periods are a shortcut for the parent folder. From the application folder, we move up to the parent folder and then to the Resources folder under the application’s folder. This is where any [...]... If you execute these statements, you will see a list such as the following in the Output window (only considerably longer): FOLDER c:\Program Files \Microsoft. NET FOLDER c:\Program Files\HTML Help Workshop FOLDER c:\Program Files \Microsoft Web Controls 0 .6 FILE c:\Program Files\folder.htt FILE c:\Program Files\desktop.ini The My.Computer.FileSystem component doesn’t expose a method to retrieve folders... addition, the GetTempFile method creates a zero-length file on the disk, which you can open with the Open method A typical temporary filename is the following: C:\DOCUME˜1\TOOLKI˜1\LOCALS˜1\Temp\tmp105.tmp 565 566 CHAPTER 15 ACCESSING FOLDERS AND FILES It was returned by the following statement on my system: Debug.WriteLine(Path.GetTempFile) The GetTempPath method returns the system’s temporary folder All temporary... DI.GetFileSystemInfos(pattern) The FileSystemInfo objects expose a few properties, which are not new to you The Name, FullName, and Extension properties return a file’s or folder’s name, or full path, or a file’s extension, 561 562 CHAPTER 15 ACCESSING FOLDERS AND FILES respectively CreationTime, LastAccessTime, and LastWriteTime are also properties of the FileSystemInfo object, as well as the Attributes property You will notice... itemsInfo(i).Attributes And FileAttributes.Directory Then { current item is a folder } Else { current item is a file } End If The code in Listing 15 .6 retrieves all the items in the C:\Program Files folder and prints their names along with the FOLDER or FILE characterization Listing 15 .6: Processing a Folder’s Items with the FileSystemInfo Object Dim path As String = ”C:\Program Files” Dim DI As New DirectoryInfo(path)... c:\folder1\folder2\folder3 Debug.WriteLine(FI.DirectoryName()) c:\folder1\folder2\folder3 Of course, the Directory method returns an object, which you can use to retrieve other properties of the parent folder 563 564 CHAPTER 15 ACCESSING FOLDERS AND FILES The Path Class The Path class contains an interesting collection of methods, which you can think of as utilities The Path class’s methods perform simple tasks... number of basic properties of the entities they represent Notice that they’re instance objects, and you must create a new instance of the corresponding class by specifying the name of a drive/folder/file in its constructor The same three objects are returned by the GetDriveInfo, GetDirectoryInfo and GetFileInfo methods of the FileSystem object The DriveInfo Class The DriveInfo class provides basic information... not, programmers leave temporary files around HasExtension This method returns a True/False value, indicating whether a path includes a file extension VB 2008 at Work: The CustomExplorer Project The CustomExplorer application, which demonstrates the basic properties and methods of the Directory and File classes, duplicates the functionality of Windows Explorer Its user interface, shown in Figure 15.2,... Sub The ScanFolder() subroutine is surprisingly simple because it’s recursive: It calls itself again and again to iterate through all the subfolders of the specified folder The GetDirectories method 567 568 CHAPTER 15 ACCESSING FOLDERS AND FILES retrieves the names of the subfolders of the current folder and returns them as an array of strings: the allFolders array The following loop iterates through... subroutine accepts as an argument a folder path and displays the files in the folder, along with their basic properties Its code, which is shown in Listing 15.10, iterates through the array with the filenames returned by the Directory.GetFiles method and uses the FileInfo class to retrieve each file’s basic properties Listing 15.10: ShowFiles() Subroutine Sub ShowFiles(ByVal selFolder As String) ListView1.Items.Clear()... file The Stream abstracts a very basic operation: the operation of sending or receiving data Does it really make any difference whether you write to a file or send data to a web client? Technically, it’s a world of difference, but wouldn’t it be nice if we could specify the destination and then send the data (or request data from a source)? The Framework abstracts this basic operation by establishing . String) Petroutsos c15.tex V2 - 01/28 /2008 2:59pm Page 5 46 5 46 CHAPTER 15 ACCESSING FOLDERS AND FILES foundFiles = My.Computer.FileSystem.FindInFiles( ”C:Program Files”, visual basic , True, FileIO.SearchOption.SearchAllSubDirectories,. as String dir = ”C:Program Files Microsoft Visual Studio 9.0” Try My.Computer.FileSystem.CopyDirectory( dir, ”E:Copy of ” & Petroutsos c15.tex V2 - 01/28 /2008 2:59pm Page 544 544 CHAPTER. Page 544 544 CHAPTER 15 ACCESSING FOLDERS AND FILES My.Computer.FileSystem.GetName(dir), Microsoft. VisualBasic.FileIO. UIOption.AllDialogs, FileIO.UICancelOption.ThrowException) Catch ex As Exception MsgBox(ex.Message) End

Ngày đăng: 12/08/2014, 21:20

Tài liệu cùng người dùng

Tài liệu liên quan