【问题标题】:copy files from one location to another location using VB and based on created date使用 VB 并根据创建日期将文件从一个位置复制到另一个位置
【发布时间】:2013-11-19 19:42:11
【问题描述】:

我正在尝试根据创建日期将 csv 文件从一个位置移动到另一个位置。

但是我使用的代码不起作用,它显示错误 sample.csv 已被另一个进程使用,我该如何解决这个问题?

这是错误消息“错误:System.Reflection.TargetInvocationException:调用的目标已引发异常。---> System.IO.IOException:进程无法访问文件'C:\新文件夹\Test.txt' 因为它正被另一个进程使用。 在 System.IO.__Error.WinIOError(Int32 错误代码,字符串可能全路径) 在 System.IO.File.InternalCopy(字符串源文件名,字符串 destFileName,布尔覆盖) 在 ST_b8571e8d94a54a80ab50a1e221d93b11.vbproj.ScriptMain.Main() --- 内部异常堆栈跟踪结束 --- 在 System.RuntimeMethodHandle._InvokeMethodFast(对象目标,Object[] 参数,SignatureStruct& sig,MethodAttributes methodAttributes,RuntimeTypeHandle typeOwner) 在 System.RuntimeMethodHandle.InvokeMethodFast(对象目标,对象 [] 参数,签名 sig,MethodAttributes 方法属性,RuntimeTypeHandle typeOwner) 在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object[] 参数,CultureInfo 文化,布尔型 skipVisibilityChecks) 在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object[] 参数,CultureInfo 文化) 在 System.RuntimeType.InvokeMember(字符串名称、BindingFlags bindingFlags、Binder binder、Object 目标、Object[] providedArgs、ParameterModifier[] 修饰符、CultureInfo 文化、String[] namedParams) 在 System.Type.InvokeMember(字符串名称,BindingFlags invokeAttr,Binder binder,Object 目标,Object[] args,CultureInfo 文化) 在 Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()"

 Public Sub Main()
        Dim csvFilesToday = New List(Of String)
        Dim sourceDir As String = "C:\New folder"
        Dim backupDir As String = "C:\New folder (2)"

        For Each csv In Directory.GetFiles(sourceDir, "*.csv", IO.SearchOption.AllDirectories)
            If File.GetCreationTime(csv).Date = Date.Today Then
                File.Copy(Path.Combine(sourceDir, csv), Path.Combine(backupDir, csv), True)
            End If
        Next
        Dts.TaskResult = ScriptResults.Success
    End Sub

End Class

【问题讨论】:

  • 确切的错误是什么?

标签: vb.net ssis


【解决方案1】:

您必须确定持有文件锁定的进程。

我有一篇关于如何使用流程资源管理器完成此任务的文章:File in use by another process

不过,如果您打开 Excel,则快速关闭它。否则,请检查您的文本编辑器

【讨论】:

    【解决方案2】:

    这不会解决您的问题,但会改进您的代码:

     Public Sub Main()
        Dim sourceDir As New DirectoryInfo("C:\New folder")
        Dim backupDir As String = "C:\New folder (2)"
    
        'Using DirectoryInfo.EnumerateFiles will reduce the total number of disk accesses
        For Each csv In sourceDir.EnumerateFiles("*.csv", SearchOption.AllDirectories.Where(Function(i) i.CreationTime >= Date.Today)
            Try
                File.Copy(csv.FullName, Path.Combine(backupDir, csv.Name), True)
            Catch Ex As IOException
                'Do something with the error here
    
            End Try
        Next csv
    
        Dts.TaskResult = ScriptResults.Success
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2013-01-01
      • 1970-01-01
      • 2019-12-27
      • 1970-01-01
      • 2015-01-30
      • 2013-05-02
      • 1970-01-01
      相关资源
      最近更新 更多