Vb.net file.copy - 仅将1个文件复制到目标文件夹 [英] Vb.net file.copy - only copying 1 file to target folder

查看:325
本文介绍了Vb.net file.copy - 仅将1个文件复制到目标文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个文本文件夹,我想复制到另一个文件夹。直接向前vb复制。当我单步执行代码时没问题,他们会复制。



当我运行exe时,只复制1个文件。



有没有人遇到过这个问题。 vb的版本是visual studio 2013.我会问是否有另一种方法可以复制,但我已经尝试过并遇到同样的问题。



必须是我的东西做错了。



帮助



我的尝试:



Hi,

I have a folder of text files that I want to copy to another folder. Straight forward vb copy. When I step through the code, no problem, they copy.

When I run the exe, only copies 1 file.

Has anyone came across this problem. The version of vb is visual studio 2013. I would ask if there is another way to copy but I have tried and get the same problem.

Must be something I am doing wrong.

Help

What I have tried:

Dim sSourceDir As String = "C:\Source\"
        Dim sTargetDir As String = "C:\Target\"        
Dim sTextList As String() = Directory.GetFiles(sSourceDir, "*.txt")

        'copy text files
        For Each f As String In sTextList
            'Remove path from the file name.
            Dim fName As String = f.Substring(sSourceDir.Length)

            ' Use the Path.Combine method to safely append the file name to the path.
            ' Will overwrite if the destination file already exists.
            File.Copy(Path.Combine(sSourceDir, fName), Path.Combine(sTargetDir, fName), True)

            '    'Make sure the file is copied, 10 second delay.
            Threading.Thread.Sleep(10000)
        Next

推荐答案

我在C#中有一个函数将文件从一个目录复制到另一个目录。

可能是它可以帮到你。



I have a function in C# to copy files from one directory to other directory.
May be it can help you.

public List<string> CopyFiles(string sourceDirectory, string destinationDirectory)
       {
           List<string> CopiedFiles = new List<string>();

           //Get the files from source directory to copy them on new location
           string[] sourceFiles = GetFilesInDirectory(sourceDirectory);


           //Make sure the destination directory exists.
           if (!DirectoryExists(destinationDirectory))
               CreateADirectory(destinationDirectory);



           int ImageCounter = 0;
           while (ImageCounter < sourceFiles.Length)
           {
               FileInfo SourceFile = new System.IO.FileInfo(sourceFiles[ImageCounter]);
               string imagepath = destinationDirectory + "/" + SourceFile.Name;


               //Check for valid extention.
               bool isValidExtnsion = false;
               Array itemValues = System.Enum.GetValues(typeof(ValidImageExtensions));
               int i = 0;
               while (i < itemValues.Length)
               {
                   if (SourceFile.Extension.ToLower() == "." + itemValues.GetValue(i))
                       isValidExtnsion = true;

                   i++;
               }


               //Copy file with valid extensions.
               if (isValidExtnsion)
               {
                   //if (!FileExists(imagepath))
                   {
                       SourceFile.CopyTo(imagepath, true);
                       CopiedFiles.Add(imagepath);
                   }
               }
               ImageCounter++;
           }

           return CopiedFiles;
       }


这篇关于Vb.net file.copy - 仅将1个文件复制到目标文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆