2012-10-25 27 views
0

所以我开发一个桌面清洁剂,其移动取决于扩展项目的具体目录(即正在清洁的那个)使用文本目录vb.net

用户定义的移动文件与某些扩展项目将被放置的文件夹的名称。

但移动文件的代码无法正常工作。

Private Sub FlowButton1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FlowButton1.Click 
    If CheckBox1.Checked Then 
     Dim folderPathTextBox = options.TextBox1.Text 
     Dim files() As String 
     files = System.IO.Directory.GetFiles(options.FolderBrowserDialog1.SelectedPath, "*.txt") 

     For Each file As String In files 
      System.IO.File.Copy(file, options.TextBox1.Text & options.TextBox8.Text & options.TextBox2.Text & System.IO.Path.GetFileName(file)) 
     Next 

    Else 
    End If 
  • options.TextBox1.Text =指南使用文件夹对话框EG选择: “C:\目录”

  • options.TextBox8.Text =斜线分开= “C:\目录\”

  • options.TextBox2.Text =文件夹名称由用户确定EG Images =“C:\ directory \ images”

我也希望代码检查文件夹是否存在,如果不创建它。

感谢任何帮助表示赞赏

+0

围绕File.Copy使用try-catch块来防止未处理的异常。打印“option.Textbox1.text .....”行并检查路径是否正确。 – Jaxedin

回答

0

一些想法,以帮助您整理/简化代码:

  1. 名称您TextBox“ES正常。如果你不能这样做,则声明有意义的局部变量并在代码中使用它。这将大大提高可读性。
  2. 使用Path.Combine加入C:\directoryImages在一起。摆脱TextBox8
  3. 我想你也可以安全地放下System.IO.Path.GetFileName(file)部分,应该能够将目录作为复制目标。File.Copy需要一个文件名作为第二个参数。您需要使用接受3个参数的Path.Combine重载,并将基本路径+用户指定的文件夹+文件名称组合在一起。
  4. 当使用File.Copy时,请确保您没有该文件在目标中。根据MSDN,Overwriting a file of the same name is not allowed
+0

非常感谢;)唯一不是真正正确的是丢弃“System.IO.Path.GetFileName(file)”,因为我需要文件名来移动它 – user1086384

+0

@ user1086384:你说得对,'File。 Copy'需要两个文件名,第二个参数'不能是目录'。对不起,在发布之前我没有很好地阅读MSDN。 :)我很高兴你能解决它! – Neolisk

0

迭代直通文件的文件夹中的另一个好方法是使用的DirectoryInfo 昏暗二作为DirectoryInfo的

二= My.Computer.FileSystem.GetDirectoryInfo(“路径到目录”) 对于每个F作为FileInfo在di.GetFiles(“*。txt”) '您有FileInfo对象的各种选项,包括复制。 Next

DirectoryInfo还提供Exists属性来确定您的dir是否真的在那里!

0

我正在寻找将子文件夹的所有图像移动到新文件夹的方法。 这是我想出来工作。

Dim WantedExtention As String = ".Your Type" 
    Dim sourcePath As String 
    Dim destinationPath As String 

    'Somewhere else in the code to set the path is by paste not folder selection. 
    FolderInfo = New DirectoryInfo(txtSelectedPath.Text) 
    SelectedFolder = txtSelectedPath.Text 
    Try 
     'Check if it exits (Why its not False, Not too sure, but this worked) 
     If Directory.Exists(SelectedFolder + "Move Folder") = True Then 
      Directory.CreateDirectory(SelectedFolder + "Move Folder") 
     End If 
     destinationPath = Path.Combine(SelectedFolder + "Move Folder") 

     For Each subdir In FolderInfo.GetDirectories() 
      'Since I am making the sub folder in the Root Folder I had to Skip the folder 
      If (subdir.Name = "Move Folder") Then 
       Exit For 
      End If 
      sourcePath = Path.Combine(SelectedFolder, subdir.Name) 

      Dim picList As String() = Directory.GetFiles(sourcePath, "*" + WantedExtention) 

      For Each f As String In picList 
       Dim fname As String = f.Substring(sourcePath.Length + 1)'Don't know why this is hear yet - but ya need it 
       FileCopy(Path.Combine(sourcePath, fname), Path.Combine(destinationPath, fname)) 
      Next 

     Next 
     lblCompleted.Text = "COMPLETED" 
    Catch ex As Exception 
     MessageBox.Show(ex.Message) 
    End Try