回答
看一看File.Copy()
使用File.Copy您可以指定新的文件名作爲目標字符串的一部分。
因此,像
File.Copy(@"c:\test.txt", @"c:\test\foo.txt");
也How to: Copy, Delete, and Move Files and Folders (C# Programming Guide)
重要的是要注意的是,您可以通過將第三個參數添加爲true或false來指定目標文件是否必須被覆蓋。 – Arman 2016-10-10 09:51:39
它不支持2服務器不同 – 2017-02-10 02:37:18
見你也可以使用File.Copy
複製和File.Move
它後記重命名。
// Copy the file (specify true or false to overwrite or not overwrite the destination file if it exists.
File.Copy(mySourceFileAndPath, myDestinationFileAndPath, [true | false]);
// EDIT: as "astander" notes correctly, this step is not necessary, as File.Copy can rename already...
// However, this code could be adapted to rename the original file after copying
// Rename the file if the destination file doesn't exist. Throw exception otherwise
//if (!File.Exists(myRenamedDestinationFileAndPath))
// File.Move(myDestinationFileAndPath, myRenamedDestinationFileAndPath);
//else
// throw new IOException("Failed to rename file after copying, because destination file exists!");
編輯
註釋掉「重命名」代碼,因爲File.Copy
已經可以複製並在一個步驟重新命名,如astander在評論正確地指出。
但是,如果OP想要在源文件被複制到新位置後重命名源文件,則可以修改重命名代碼。
你不必複製和重命名,你可以使用File.Copy – 2009-12-30 12:33:15
一步到位做到這一點有道理......我最近使用File.Move做了很多文件重命名我甚至沒有想到File.Copy能夠重命名:-) – 2009-12-30 12:35:07
File :: Copy將文件複製到目標文件夾,File :: Move可以同時移動和重命名文件。
是的。它會工作:FileInfo.CopyTo Method
使用此方法來允許或防止覆蓋現有的文件。使用CopyTo方法可防止默認覆蓋現有文件。
所有其他的反應是正確的,但既然你問了FileInfo
,這裏有一個例子:
FileInfo fi = new FileInfo(@"c:\yourfile.ext");
fi.CopyTo(@"d:\anotherfile.ext", true); // existing file will be overwritten
我試圖從一個位置複製一個XML文件到另一個。這裏是我的代碼:
public void SaveStockInfoToAnotherFile()
{
string sourcePath = @"C:\inetpub\wwwroot";
string destinationPath = @"G:\ProjectBO\ForFutureAnalysis";
string sourceFileName = "startingStock.xml";
string destinationFileName = DateTime.Now.ToString("yyyyMMddhhmmss") + ".xml"; // Don't mind this. I did this because I needed to name the copied files with respect to time.
string sourceFile = System.IO.Path.Combine(sourcePath, sourceFileName);
string destinationFile = System.IO.Path.Combine(destinationPath, destinationFileName);
if (!System.IO.Directory.Exists(destinationPath))
{
System.IO.Directory.CreateDirectory(destinationPath);
}
System.IO.File.Copy(sourceFile, destinationFile, true);
}
然後我打電話一定的時間間隔,我認爲你不需要看到的timer_elapsed函數內部此功能。有效。希望這可以幫助。
來複制我用兩個文本框要知道文件夾和地點花葯文本框知道什麼文件夾複製它的文件夾,這是代碼
MessageBox.Show("The File is Create in The Place Of The Programe If you Don't Write The Place Of copy And You write Only Name Of Folder");// It Is To Help The User TO Know
if (Fromtb.Text=="")
{
MessageBox.Show("Ples You Should Write All Text Box");
Fromtb.Select();
return;
}
else if (Nametb.Text == "")
{
MessageBox.Show("Ples You Should Write The Third Text Box");
Nametb.Select();
return;
}
else if (Totb.Text == "")
{
MessageBox.Show("Ples You Should Write The Second Text Box");
Totb.Select();
return;
}
string fileName = Nametb.Text;
string sourcePath = @"" + Fromtb.Text;
string targetPath = @"" + Totb.Text;
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
//when The User Write The New Folder It Will Create
MessageBox.Show("The File is Create in "+" "+Totb.Text);
}
System.IO.File.Copy(sourceFile, destFile, true);
if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);
foreach (string s in files)
{
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(s, destFile, true);
}
MessageBox.Show("The File is copy To " + Totb.Text);
}
這是我做的移動從下載到桌面的測試文件。 我希望它有用。
private void button1_Click(object sender, EventArgs e)//Copy files to the desktop
{
string sourcePath = @"C:\Users\UsreName\Downloads";
string targetPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string[] shortcuts = {
"FileCopyTest.txt"};
try
{
listbox1.Items.Add("Starting: Copy shortcuts to dektop.");
for (int i = 0; i < shortcuts.Length; i++)
{
if (shortcuts[i]!= null)
{
File.Copy(Path.Combine(sourcePath, shortcuts[i]), Path.Combine(targetPath, shortcuts[i]), true);
listbox1.Items.Add(shortcuts[i] + " was moved to desktop!");
}
else
{
listbox1.Items.Add("Shortcut " + shortcuts[i] + " Not found!");
}
}
}
catch (Exception ex)
{
listbox1.Items.Add("Unable to Copy file. Error : " + ex);
}
}
string directoryPath = Path.GetDirectoryName(destinationFileName);
// If directory doesn't exist create one
if (!Directory.Exists(directoryPath))
{
DirectoryInfo di = Directory.CreateDirectory(directoryPath);
}
File.Copy(sourceFileName, destinationFileName);
- 1. 如何從一個路徑複製文件夾並將其粘貼到使用c#的另一個路徑?
- 2. 如何從一個路徑在C++中的文件夾複製到另一個
- 3. 從一個路徑複製一個特定的文件到另一個
- 4. 如何使用批處理文件將多個文件夾複製到另一個路徑?
- 5. 如何將文件內容複製到另一個文件?
- 6. 將路徑/文件列表複製到一個目錄
- 7. 將文件(img/doc)從特定路徑複製到另一個Android
- 8. 如何將一個文件複製到另一個vb.net 2008
- 9. 如何將文件從一個EBS複製到另一個?
- 10. C# - 如何從一個路徑複製SQLite數據庫並將其粘貼到另一個路徑?
- 11. 如何將文件從一個文件夾複製到另一個文件夾
- 12. 從另一個表格複製文件路徑輸入
- 13. 將png文件從一個文件夾複製到另一個
- 14. 將x文件從一個文件夾複製到另一個
- 15. 如何複製文件並將完整路徑粘貼到JTextField
- 16. 如何使用webpack將文件夾複製到公共路徑?
- 17. 如何將文件複製到特定路徑?
- 18. 如何將文件從一個文件夾複製到另一個
- 19. 如何將文件複製到cordva中的另一個目錄?
- 20. 如何使用shell腳本將文件從一個路徑移動到unix中的另一個路徑
- 21. 如何剪切和複製文件夾中的文件到PHP 5.2中的另一個路徑?
- 22. jQuery SVG複製到另一個SVG的路徑
- 23. 如何從一個絕對到另一個找到相對的文件路徑?
- 24. 將mp3文件的數據複製到另一個文件。 C
- 25. 要將zip文件複製到另一個zip文件
- 26. 將文件複製到另一個文件夾
- 27. 如何將xml的一部分複製到另一個文檔
- 28. 將文件從一個目錄複製到另一個目錄
- 29. 將3gp moov原子從一個文件複製到另一個
- 30. 將圖像從一個文件夾複製到另一個
你要重命名的原件或新複製的文件嗎? – 2009-12-30 12:34:36
重命名新複製的文件 – mrblah 2009-12-30 15:42:50