2011-10-19 49 views
0

使用VB.Net如何複製和保存文件

我想將txt文件從一個位置複製到另一個位置。

textbox1.text = Source file directory 
textbox2.text = destination file directory 

對於實施例

textbox1.text = "C:\1.txt" 
textbox2.text = "d:\" 

我想的1.txt到d驅動器複製。

如何做到這一點。

需要示例代碼幫助

回答

4

使用File.Copy方法。

File.Copy(source, destination) 

或者,如果文件在目標位置已經存在,指定一個布爾值指示是否覆蓋,

File.Copy(source, destination, overwrite) 
1

這是很簡單的:

File.Copy(textbox1.Text, Path.Combine(textbox2.Text, Path.GetFileName(textbox1.Text))) 
+0

+1 :我沒有看到文本文件名在textbox2.text(加上沒有在textbox2文本Camelcasing):) – naveen

+1

@naveen:正確的...因爲它只是一個路徑,你必須從源獲取文件名。我補充說。 – Guffa