This is achieved using the Visual Basic FileStream class. The FileStream constructor accepts the file name to be opened as the first parameter, followed by a number of other parameters defining the mode in which the file is to be opened.
The options available as listed in the following tables:. OpenOrCreate with FileAccess. ReadWrite permission and no file sharing, and then closes it:. If it does not, the code will fail. Once a file has been opened with the appropriate options, it can be written to using the Visual Basic StreamWriter class. The StreamWriter constructor takes a FileStream as the sole parameter. Write writes the text with no new line appended to the end of each line.
WriteLine on the other hand, appends a new line to end of each line written to the file. In the following code excerpt a StreamWriter object is created using the FileStream, and a For loop writes 11 lines to the file:. Note that since the act of closing causes the operating system buffers to be flushed to the file, failing to close the Writer and Stream objects before the application exits will result in data written to the file being lost.
Now that we have created and written to a file the next step is to read some data from the file. This is achieved using the Visual Basic StreamReader object. The file exists and is read-only IOException. The disk is full IOException. The pathname is too long PathTooLongException.
This example creates a new file, if the file does not already exist. If an application needs to create a file, that application needs Create access for the folder. If the file already exists, the application needs only Write access, a lesser privilege. Where possible, it is more secure to create the file during deployment, and only grant Read access to a single file, rather than Create access for a folder. If you are running in a partial-trust context, the code might throw an exception due to insufficient privileges.
For more information, see Code Access Security Basics. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode. Is this page helpful? Please rate your experience Yes No.
0コメント