VB.net如何将文件复制到新位置 [英] VB.net How To Copy File To New Location

查看:85
本文介绍了VB.net如何将文件复制到新位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在从事一个项目,目的是允许我的用户从那里的个人文件中选择一个mp3/wav文件,并将其保存在我的程序目录中,以便以后可以访问.

So im working on a project, with the aim to allow my user to select a mp3/wav file from there personal files, and save it in my programs directory so it can be accessed later.

Public Class Uploader_Control


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button_Upload.Click 'Button click triggers open file dialog
    OpenFileDialog1.InitialDirectory = "C:\" 
    OpenFileDialog1.Title = "Upload A Song" 
    OpenFileDialog1.Filter = "Audio Files|*.mp3; *.wav"
    OpenFileDialog1.Multiselect = False
    OpenFileDialog1.ShowDialog()
    Dim New_Upload As String = OpenFileDialog1.FileName 'Sets 'New_Upload' to the location of the selected file.

    Dim Upload_Path = Get_Upload_Path() 'Triggers function to find the destination path that i want the file to be copied too



End Sub
Function Get_Upload_Path() 'Tries to find the location for the file to saved too
    Dim Upload_Path As String = Application.ExecutablePath
    Upload_Path = Upload_Path.Substring(0, Upload_Path.LastIndexOf("/")) & "\Resources\" 

    Return Upload_Path
End Function

我尝试了几种方法尝试将其复制,但是我似乎找不到找到将文件复制到目录中的方法.如果任何人都能对我实现这一目标的方法有任何见解,将不胜感激.

I have tried a few methods to try get it to copy however i cant seem to find a way to copy the file into a directory. If anyone can give any insight into a way i can achieve this it would be greatly appreciated.

推荐答案

首先要解决的是您的GetUploadPath.

The first thing to fix is your GetUploadPath.

Function Get_Upload_Path() as String
    Dim newPath As String = Application.ExecutablePath
    newPath = Path.Combine(Path.GetDirectoryName(newPath), "Resources")
    Return newPath
End Function

在这里,我正在使用安全的Path类方法来提取您的文件夹并附加您的Resource文件夹.您的代码改为使用字符"/",这不是Windows中的默认路径分隔符.可能在那里会出现异常

Here I am using the safe Path class methods to extract your folder and appending your Resource folder. Your code instead uses the character "/" and this is not the default path separator in Windows. Probably you will get an exception there

最后,您使用File.Copy复制文件,但请记住,应始终为File.Copy提供文件名.复制源和目标参数,而不仅仅是目录名

Finally you use File.Copy to copy a file, but remember, you should always give a filename to File.Copy source and destination parameters, not just a directory name

  if OpenFileDialog1.ShowDialog() = DialogResult.OK Then
     Dim newPath = Get_Upload_Path()
     Dim file = Path.GetFileName(OpenFileDialog1.FileName)
     Dim newUploadName = Path.Combine(newPath, file)
     File.Copy(OpenFileDialog1.FileName, newUploadName)
  End If

这篇关于VB.net如何将文件复制到新位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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