を使用File.Copy(source, target, True)
しました。ここで、source
はのようなフルパス名でc:\source.txt
ありtarget
、はフォルダであり、同じ名前のファイルが含まれている可能性があります。source.txt
ターゲットフォルダにコピーして、ファイルがすでに存在する場合は上書きしたい。
しかし、私はエラーが発生しました:
「ターゲットはフォルダであり、ファイルではありません」
を使用File.Copy(source, target, True)
しました。ここで、source
はのようなフルパス名でc:\source.txt
ありtarget
、はフォルダであり、同じ名前のファイルが含まれている可能性があります。source.txt
ターゲットフォルダにコピーして、ファイルがすでに存在する場合は上書きしたい。
しかし、私はエラーが発生しました:
「ターゲットはフォルダであり、ファイルではありません」
ターゲットにはファイル名も含まれている必要があります。
sSource = "C:\something.txt"
sTarget = "C:\folder\something.txt"
File.Copy(sSource, sTarget, True)
プログラムで同じファイル名を使用する場合は、次のようにします。
File.Copy(sSource, Path.Combine(sFolder, Path.GetFileName(sSource)), True)
例外とメソッドの使用に関する例と情報については、 MSDNドキュメントをお読みください。
FileStreamを使用して、ファイルの内容を読み書きすることもできます。ファイルストリームを使用すると、テキストファイルだけでなく、すべての種類のバイナリファイルを読み書きできます。
次の手順を使用します。
''' <summary>
''' copies a file from one location to another
''' </summary>
''' <param name="inputPath">full path to the input file</param>
''' <param name="outputPath">full path to the output file</param>
''' <param name="bufferSize">the size in bytes as an integer that will be read and written at a time from input file and to the output file</param>
''' <param name="overwrite">overwrite the output file if it already exists</param>
''' <returns></returns>
Public Function CopyFile(ByVal inputPath As String,
ByVal outputPath As String,
ByVal bufferSize As Integer,
Optional ByVal overwrite As Boolean = False) As Boolean
Dim PathIsClear As Boolean = True, SucOpt As Boolean = False
Dim inputByteReaderObj As System.IO.FileStream = System.IO.File.Open(inputPath, IO.FileMode.Open) 'open a file stream for reading bytes from the input file
Dim endofSize As Integer = My.Computer.FileSystem.GetFileInfo(inputPath).Length
If overwrite AndAlso FileExists(outputPath) Then
'if file exits, delete the output file
PathIsClear = False
PathIsClear = DeleteFilesOnDisk(outputPath) ' Delete the output file if it already exists
End If
' Adjust array length for VB array declaration.
Dim allBytesRead As Integer = 0, sucessfullBytes As Integer = 0, bytes As Byte() 'The byte array
If bufferSize > endofSize Then
bufferSize = endofSize
End If
If bufferSize >= 1 Then
bytes = New Byte(bufferSize - 1) {} 'The byte array; create a byte array that will hold the data in length equal to the bufferSize; the array index starts at 0;
If PathIsClear Then
While inputByteReaderObj.Read(bytes, 0, bufferSize) > 0
'read bytes consequtively from the input file, each time read bytes equal in length to bufferSize
Try
My.Computer.FileSystem.WriteAllBytes(outputPath, bytes, True) ' Append to the file contents
sucessfullBytes += bufferSize
Catch ex As Exception
Stop
End Try
allBytesRead += bufferSize
If (allBytesRead + bufferSize) > endofSize Then
bufferSize = endofSize - allBytesRead 'change the size of the buffer match the end of the file
End If
If bufferSize >= 1 Then
bytes = New Byte(bufferSize - 1) {} 'the array index starts at zero, the bufferSize starts at 1
End If
If allBytesRead >= endofSize Or bufferSize = 0 Then
'the reader has already reached the end of file, exit the reader loop
Exit While
End If
End While
If sucessfullBytes = allBytesRead Then
SucOpt = True
End If
End If
Else
'write an empty file
Try
System.IO.File.Create(outputPath) 'Create an empty file because the size of the input file is zero
Catch ex As Exception
'an error occured in creating an empty file
End Try
End If
inputByteReaderObj.Close()
Return SucOpt
End Function