Tải bản đầy đủ (.docx) (7 trang)

Giáo trình Visual Basic 8

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (98.31 KB, 7 trang )

Members
Member Description
Default 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.
Remarks
When 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.
Members
Member Description
Append 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.
Remarks
When 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.
Members
Member Description
Default 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.
Requirements
Private 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 If
End Sub
The 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 Code
My.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 Code
My.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 Code
My.Computer.Clipboard.SetData("specialFormat",
dataChunk)
See Also
Concepts
How to: Read from the Clipboard in Visual Basic
How to: Determine What Type of File is Stored on the Clipboard in Visual Basic
My.Computer.Clipboard Object
My.Computer.Clipboard.SetText Method
My.Computer.Clipboard.SetData Method
My.Computer.Clipboard.SetDataObject Method
Reference
TextDataFormat
You 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 Code
Dim infoReader As System.IO.FileInfo
infoReader =
My.Computer.FileSystem.GetFileInfo("C:\testfile.txt")
MsgBox("File is " & infoReader.Length & " bytes.")
See Also
Concepts
My.Computer.FileSystem Object
My.Computer.FileSystem.GetFileInfo Method
File, Directory, and Drive Properties in Visual Basic
How to: Determine a File's Attributes in Visual Basic
The following table lists examples of tasks involving the My.Computer.FileSystem object.
To See
Read from a text file How to: Read From Text Files in Visual
Basic
Read from a delimited text
file
How to: Read From Comma-Delimited
Text Files in Visual Basic
Read from a fixed-width text
file
How to: Read From Fixed-width Text
Files in Visual Basic
Read from a text file with
multiple formats
How to: Read From Text Files with
Multiple Formats in Visual Basic
Read from a binary file How to: Read From Binary Files in
Visual Basic

Read from text files in the
MyDocuments directory
How to: Read From Existing Text Files
in My Documents (Visual Basic)
Read from a text file with a
StreamReader
How to: Read Text from Files with a
StreamReader (Visual Basic)
Write to a text file How to: Write Text to Files in Visual
Basic
Append to a text file How to: Append to Text Files in Visual
Basic
Write to a binary file How to: Write to Binary Files in Visual
Basic
Write to text files in the
MyDocuments directory
How to: Write Text to Files in the My
Documents Directory in Visual Basic
Write to a text file with a
StreamWriter
How to: Write Text to Files with a
StreamWriter in Visual Basic
Copy files with a specific
pattern
How to: Copy Files with a Specific
Pattern to a Directory in Visual Basic
Copy a file to the same How to: Create a Copy of a File in the

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×