本文整理汇总了VB.NET中System.IO.FileInfo.CopyTo方法的典型用法代码示例。如果您正苦于以下问题:VB.NET FileInfo.CopyTo方法的具体用法?VB.NET FileInfo.CopyTo怎么用?VB.NET FileInfo.CopyTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.FileInfo
的用法示例。
在下文中一共展示了FileInfo.CopyTo方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Test
' 导入命名空间
Imports System.IO
Public Class Test
Public Shared Sub Main()
'Specify the directories you want to manipulate.
Dim path As String = "c:\SourceFile.txt"
Dim path2 As String = "c:\NewFile.txt"
Dim fi As FileInfo = New FileInfo(path)
Dim fi2 As FileInfo = New FileInfo(path2)
Try
Using fs As FileStream = fi.Create()
End Using
'Ensure that the target does not exist.
If File.Exists(path2) Then
fi2.Delete()
End If
'Copy the file.
fi.CopyTo(path2)
Console.WriteLine("{0} was copied to {1}.", path, path2)
Catch ioex As IOException
Console.WriteLine(ioex.Message)
End Try
End Sub
End Class
示例2: CopyToTest
' 导入命名空间
Imports System.IO
Public Class CopyToTest
Public Shared Sub Main()
Try
' Create a reference to a file, which might or might not exist.
' If it does not exist, it is not yet created.
Dim fi As New FileInfo("temp.txt")
' Create a writer, ready to add entries to the file.
Dim sw As StreamWriter = fi.AppendText()
sw.WriteLine("Add as many lines as you like...")
sw.WriteLine("Add another line to the output...")
sw.Flush()
sw.Close()
' Get the information out of the file and display it.
Dim sr As New StreamReader(fi.OpenRead())
Console.WriteLine("This is the information in the first file:")
While sr.Peek() <> -1
Console.WriteLine(sr.ReadLine())
End While
' Copy this file to another file.
Dim newfi As FileInfo = fi.CopyTo("newTemp.txt")
' Get the information out of the new file and display it.
sr = New StreamReader(newfi.OpenRead())
Console.WriteLine("{0}This is the information in the second file:", Environment.NewLine)
While sr.Peek() <> -1
Console.WriteLine(sr.ReadLine())
End While
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
End Class
'This code produces output similar to the following;
'results may vary based on the computer/file structure/etc.:
'
'This is the information in the first file:
'Add as many lines as you like...
'Add another line to the output...
'
'This is the information in the second file:
'Add as many lines as you like...
'Add another line to the output...
示例3: CopyToTest
' 导入命名空间
Imports System.IO
Public Class CopyToTest
Public Shared Sub Main()
' Create a reference to a file, which might or might not exist.
' If it does not exist, it is not yet created.
Dim fi As New FileInfo("temp.txt")
' Create a writer, ready to add entries to the file.
Dim sw As StreamWriter = fi.AppendText()
sw.WriteLine("Add as many lines as you like...")
sw.WriteLine("Add another line to the output...")
sw.Flush()
sw.Close()
' Get the information out of the file and display it.
Dim sr As New StreamReader(fi.OpenRead())
Console.WriteLine("This is the information in the first file:")
While sr.Peek() <> -1
Console.WriteLine(sr.ReadLine())
End While
' Copy this file to another file. The true parameter specifies
' that the file will be overwritten if it already exists.
Dim newfi As FileInfo = fi.CopyTo("newTemp.txt", True)
' Get the information out of the new file and display it.
sr = New StreamReader(newfi.OpenRead())
Console.WriteLine("{0}This is the information in the second file:", Environment.NewLine)
While sr.Peek() <> -1
Console.WriteLine(sr.ReadLine())
End While
End Sub
End Class
'This code produces output similar to the following;
'results may vary based on the computer/file structure/etc.:
'
'This is the information in the first file:
'Add as many lines as you like...
'Add another line to the output...
'
'This is the information in the second file:
'Add as many lines as you like...
'Add another line to the output...
'