Giáo trình Visual Basic 8

7 1.4K 8
Giáo trình Visual Basic 8

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

Thông tin tài liệu

Giáo trình Visual Basic

MembersMember DescriptionDefault Read and write access permitted. This is the default.Read Read access permitted.ReadWrite Read and write access permitted.Write Write access permitted.Indicates how to open a file when calling file-access functions.RemarksWhen you call file access–related functions, you can use enumeration members in your code in place of the actual values.The OpenMode enumeration defines constants used to set file access modes. The following table lists the OpenMode enumeration members.Input, Output, and Append are used when sequentially accessing files, such as text files, while Binary is used for binary file access and Random for random file access.When sequentially accessing a file, you cannot change its data. You can read the data, append to it, or overwrite it with new data. If you open it for input, the contents of the file will be overwritten, even if you do not directly write to the file.When performing file I/O operations, the My.Computer.FileSystem object provides greater performance and ease of use than legacy file I/O methods. For more information, see My.Computer.FileSystem Object.MembersMember DescriptionAppend File opened to append to it. Default.Binary File opened for binary access.Input File opened for read access.Output File opened for write access.Random File opened for random access.Indicates how to open a file when calling file-access functions. RemarksWhen you call file access–related functions, you can use enumeration members in your code in place of the actual values.The OpenShare enumeration defines constants that specify whether or not other processes can access the file while your application has it open. The following table lists the OpenShare enumeration members.When performing file I/O operations, the My.Computer.FileSystem object provides greater performance and ease of use than legacy file I/O methods. For more information, see My.Computer.FileSystem Object.MembersMember DescriptionDefault LockReadWrite. This is the default.LockRead Other processes cannot read the file.LockReadWrite Other processes cannot read or write to the file.LockWrite Other processes cannot write to the file.Shared Any process can read or write to the file.RequirementsPrivate Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim myStream As Stream = Nothing Dim openFileDialog1 As New OpenFileDialog() openFileDialog1.InitialDirectory = "c:\" openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" openFileDialog1.FilterIndex = 2 openFileDialog1.RestoreDirectory = True If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then Try myStream = openFileDialog1.OpenFile() If (myStream IsNot Nothing) Then ' Insert code to read the stream here. End If Catch Ex As Exception MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message) Finally ' Check this again, since we need to make sure we didn't throw an exception on open. If (myStream IsNot Nothing) Then myStream.Close() End If End Try End IfEnd SubThe Clipboard can be used to store data, such as text and images. Because the Clipboard is shared by all processes, it can be used to transfer data between them. The My.Computer.Clipboard object allows you to easily access the Clipboard and to read and write to it. The SetAudio, SetData, SetFileDropDownList, SetImage, and SetText methods allow you to place data on the Clipboard.Security Note:Because the Clipboard can be accessed by other users, do not use it to store sensitive information, such as passwords or confidential data.To write text to the Clipboard• Use the My.Computer.Clipboard.SetText method to write text to the Clipboard. The following code writes the string "This is a test string" to the Clipboard.Visual Basic Copy CodeMy.Computer.Clipboard.SetText("This is a test string.")To write text to the Clipboard in a specific format• Use the My.Computer.Clipboard.SetText method to write text to the Clipboard, including the type of TextDataFormat. The following code writes the string "This is a test string" to the Clipboard as RTF text.Visual Basic Copy CodeMy.Computer.Clipboard.SetText("This is a test string.", _System.Windows.Forms.TextDataFormat.Rtf)To write data to the Clipboard• Use the My.Computer.Clipboard.SetData method to write data to the Clipboard. This example writes the DataObjectdataChunk to the Clipboard in the custom format specialFormat. Visual Basic Copy CodeMy.Computer.Clipboard.SetData("specialFormat", dataChunk)See AlsoConceptsHow to: Read from the Clipboard in Visual BasicHow to: Determine What Type of File is Stored on the Clipboard in Visual BasicMy.Computer.Clipboard ObjectMy.Computer.Clipboard.SetText MethodMy.Computer.Clipboard.SetData MethodMy.Computer.Clipboard.SetDataObject MethodReferenceTextDataFormatYou can use the Length property to determine the size of the file in bytes.Note:The options available in dialog boxes, and the names and locations of menu commands you see, might differ from what is described in Help, depending on your active settings or edition. This Help page was written with General Development Settings in mind. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.To determine a file's size• Use the GetFileInfo method to return a FileInfo object for the file, which can be queried for information. This example gets a FileInfo object for Testfile.txt and uses the Length property to display the size of the file in bytes.Visual Basic Copy CodeDim infoReader As System.IO.FileInfoinfoReader = My.Computer.FileSystem.GetFileInfo("C:\testfile.txt")MsgBox("File is " & infoReader.Length & " bytes.")See AlsoConceptsMy.Computer.FileSystem ObjectMy.Computer.FileSystem.GetFileInfo MethodFile, Directory, and Drive Properties in Visual BasicHow to: Determine a File's Attributes in Visual BasicThe following table lists examples of tasks involving the My.Computer.FileSystem object. To SeeRead from a text file How to: Read From Text Files in Visual BasicRead from a delimited text fileHow to: Read From Comma-Delimited Text Files in Visual BasicRead from a fixed-width text fileHow to: Read From Fixed-width Text Files in Visual BasicRead from a text file with multiple formatsHow to: Read From Text Files with Multiple Formats in Visual BasicRead from a binary file How to: Read From Binary Files in Visual BasicRead from text files in the MyDocuments directoryHow to: Read From Existing Text Files in My Documents (Visual Basic)Read from a text file with a StreamReaderHow to: Read Text from Files with a StreamReader (Visual Basic)Write to a text file How to: Write Text to Files in Visual BasicAppend to a text file How to: Append to Text Files in Visual BasicWrite to a binary file How to: Write to Binary Files in Visual BasicWrite to text files in the MyDocuments directoryHow to: Write Text to Files in the My Documents Directory in Visual BasicWrite to a text file with a StreamWriterHow to: Write Text to Files with a StreamWriter in Visual BasicCopy files with a specific patternHow to: Copy Files with a Specific Pattern to a Directory in Visual BasicCopy a file to the same How to: Create a Copy of a File in the directory Same Directory in Visual BasicCopy a file to a different directoryHow to: Create a Copy of a File in a Different Directory in Visual BasicCreate a file How to: Create a File in Visual BasicDelete a file How to: Delete a File in Visual BasicDelete all the files in a directoryHow to: Delete All Files in a Directory in Visual BasicFind files with a specific patternHow to: Find Files with a Specific Pattern in Visual BasicMove a file How to: Move a File in Visual BasicMove a collection of files How to: Move a Collection of Files in Visual BasicRename a file How to: Rename a File in Visual BasicRename a directory How to: Rename a Directory in Visual BasicCopy a directory to another directoryHow to: Copy a Directory to Another Directory in Visual BasicCreate a directory How to: Create a Directory in Visual BasicDelete a directory How to: Delete a Directory in Visual BasicFind subdirectories with a specific patternHow to: Find Subdirectories with a Specific Pattern in Visual BasicGet the collection of files in a directoryHow to: Get the Collection of Files in a Directory in Visual BasicDetermine how many files are in a directoryHow to: Determine How Many Files Are in a Directory in Visual Basic Move a directory How to: Move a Directory in Visual BasicMove the contents of a directoryHow to: Move the Contents of a Directory in Visual BasicRead from the MyDocuments directoryHow to: Retrieve the Contents of the My Documents Directory in Visual BasicParse a file path How to: Parse File Paths in Visual BasicExampleThis example checks to determine whether the folder C:\backup\logs exists and checks its properties.Visual Basic Copy CodeDim logInfo As System.IO.DirectoryInfoIf My.Computer.FileSystem.DirectoryExists("C:\backup\logs") Then logInfo = My.Computer.FileSystem.GetDirectoryInfo _ ("C:\backup\logs")End IfRequirements . StreamReader (Visual Basic) Write to a text file How to: Write Text to Files in Visual BasicAppend to a text file How to: Append to Text Files in Visual BasicWrite. in Visual BasicDelete a file How to: Delete a File in Visual BasicDelete all the files in a directoryHow to: Delete All Files in a Directory in Visual BasicFind

Ngày đăng: 13/11/2012, 10:23

Từ khóa liên quan

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

  • Đang cập nhật ...

Tài liệu liên quan