首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在VB.NET中将文件复制/替换到文件夹中?

如何在VB.NET中将文件复制/替换到文件夹中?
EN

Stack Overflow用户
提问于 2013-02-20 19:37:39
回答 2查看 45.6K关注 0票数 4

我使用了File.Copy(source, target, True),其中source是一个完整的路径名,就像c:\source.txt一样,target是一个文件夹,其中可能包含相同名称的文件。我希望将source.txt复制到目标文件夹,如果该文件已经存在,则覆盖该文件。

但是我得到了一个错误:

‘目标是文件夹,而不是文件’

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-20 19:40:01

目标还必须包含文件名:

代码语言:javascript
复制
sSource = "C:\something.txt"
sTarget = "C:\folder\something.txt"

File.Copy(sSource, sTarget, True)

如果您想以编程方式拥有相同的文件名,只需执行以下操作:

代码语言:javascript
复制
File.Copy(sSource, Path.Combine(sFolder, Path.GetFileName(sSource)), True)

请阅读MSDN Documentation以获得有关异常和方法用法的示例和信息。

票数 10
EN

Stack Overflow用户

发布于 2019-10-07 14:46:34

您还可以使用FileStream来读写文件内容。如果使用文件流,则可以读取和写入所有类型的二进制文件,而不仅仅是文本文件。

使用以下步骤:

代码语言:javascript
复制
''' <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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14978894

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档