本文整理汇总了VB.NET中System.IO.FileInfo.Open方法的典型用法代码示例。如果您正苦于以下问题:VB.NET FileInfo.Open方法的具体用法?VB.NET FileInfo.Open怎么用?VB.NET FileInfo.Open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.FileInfo 的用法示例。
在下文中一共展示了FileInfo.Open方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的VB.NET代码示例。
示例1: Test
' 导入命名空间
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
Dim fi As FileInfo = New FileInfo(path)
Dim fs As FileStream
' Delete the file if it exists.
If fi.Exists = False Then
'Create the file.
fs = fi.Create()
Dim info As Byte() = New UTF8Encoding(True).GetBytes("This is some text in the file.")
'Add some information to the file.
fs.Write(info, 0, info.Length)
fs.Close()
End If
'Open the stream and read it back.
fs = fi.Open(FileMode.Open)
Dim b(1023) As Byte
Dim temp As UTF8Encoding = New UTF8Encoding(True)
Do While fs.Read(b, 0, b.Length) > 0
Console.WriteLine(temp.GetString(b))
Loop
fs.Close()
End Sub
End Class
'This code produces output similar to the following;
'results may vary based on the computer/file structure/etc.:
'
'This is some text in the file.
'
'
'
'
'
'
'
'
'
'
'
'
开发者ID:VB.NET开发者,项目名称:System.IO,代码行数:47,代码来源:FileInfo.Open
示例2: Test
imports System.IO
imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\MyTest.txt"
Dim fi As FileInfo = new FileInfo(path)
Dim fs As FileStream
' Delete the file if it exists.
If fi.Exists = False
'Create the file.
fs = fi.Create()
Dim info As Byte() = _
New UTF8Encoding(true).GetBytes( _
"This is some text in the file.")
'Add some information to the file.
fs.Write(info, 0, info.Length)
fs.Close()
End If
'Open the stream and read it back.
fs = fi.Open(FileMode.Open, FileAccess.Read)
Dim b(1023) As byte
Dim temp As UTF8Encoding = New UTF8Encoding(true)
Do While fs.Read(b,0,b.Length) > 0
Console.WriteLine(temp.GetString(b))
Loop
Try
fs.Write(b,0,b.Length)
Catch e As Exception
Console.WriteLine("Writing was disallowed, as expected: {0}", e.ToString())
End Try
fs.Close()
End Sub
End Class
'This code produces output similar to the following;
'results may vary based on the computer/file structure/etc.:
'
'This is some text in the file.
'
'
'
'
'
'
'
'
'
'
'
'
'Writing was disallowed, as expected: System.NotSupportedException: Stream does
'not support writing.
' at System.IO.__Error.WriteNotSupported()
' at System.IO.FileStream.Write(Byte[] array, Int32 offset, Int32 count)
' at VB_Console_Application.Test.Main() in C:\Documents and Settings\MyComputer
'\My Documents\Visual Studio 2005\Projects\finfo open2\finfo open2\Module1.vb:line 34
'
开发者ID:VB.NET开发者,项目名称:System.IO,代码行数:61,代码来源:FileInfo.Open
示例3: OpenTest
' 导入命名空间
Imports System.IO
Public Class OpenTest
Public Shared Sub Main()
' Open an existing file, or create a new one.
Dim fi As New FileInfo("temp.txt")
' Open the file just specified such that no one else can use it.
Dim fs As FileStream = fi.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)
' Create another reference to the same file.
Dim nextfi As New FileInfo("temp.txt")
Try
' Try opening the same file, which was locked by the previous process.
nextfi.Open(FileMode.OpenOrCreate, FileAccess.Read)
Console.WriteLine("The file was not locked, and was opened by a second process.")
Catch i as IOException
Console.WriteLine(i.ToString())
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
' Close the file so it can be deleted.
fs.Close()
End Sub
End Class
'This code produces output similar to the following;
'results may vary based on the computer/file structure/etc.:
'
'System.IO.IOException: The process cannot access the file
''C:\Documents and Settings\mydirectory\My Documents\Visual Studio 2005
'\Projects\fileinfoopen\fileinfoopen\obj\Release\temp.txt'
'because it is being used by another process.
'at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
'at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access,
'Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions
'options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
'at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access,
'FileShare share) at System.IO.FileInfo.Open(FileMode mode, FileAccess access)
'at VB_Console_Application.OpenTest.Main() in C:\Documents and Settings
'\mydirectory\My Documents\Visual Studio 2005\Projects\VB_Console_Application
'\VB_Console_Application\Module1.vb:line 19
开发者ID:VB.NET开发者,项目名称:System.IO,代码行数:45,代码来源:FileInfo.Open
注:本文中的System.IO.FileInfo.Open方法示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论