[英]How can I move a user chosen file to a user chosen directory in Visual C#?
我正在嘗試制作一個程序,該程序可以將用戶選擇的任何文件移動到另一個文件夾中。 我環顧四周,收集了一些信息,然后將它們放在一起,但這並不能使工作安靜下來,但是也沒有出現任何錯誤。 有人可以告訴我它有什么問題嗎,或者如何解決? (這是我對Visual Studio + C#的首次測試)
這是代碼(在按鈕內):
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string sourceFile = openFileDialog1.InitialDirectory + openFileDialog1.FileName;
var dirPath = @"C:\Users\goverpie\Desktop\Server\Plugins\";
var extn = Path.GetExtension(sourceFile);
var finalName = sourceFile + extn;
var targetFilePath = Path.Combine(dirPath, sourceFile);
File.Move(sourceFile, targetFilePath);
}
因此,該項目構建並正常運行,並打開了對話框,但是當我選擇文件時,該對話框將正常關閉並且什么也沒有移動。
謝謝!
你sourceFile
字符串包含.InitialDirectory
以及.FileName
,所以(如果我讀這正確),選擇C:\\ TEMP \\ myFile.foo為目標文件時, targetFilePath
將結束是C:\\用戶\\ goverpie \\桌面\\服務器\\插件\\ C:\\ TEMP \\ myFile.foo 。 假設系統在路徑中找到:
時將引發錯誤,則您可能不處理被引發的異常,並且系統僅在退出。
在File.Move(sourceFile, targetFilePath);
處放置一個斷點File.Move(sourceFile, targetFilePath);
線,看看什么是值。 另外,將整個內容包裝在try/catch
塊中並捕獲異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.