2016-06-21 53 views
0

首先,我真的想自己解決這個問題,但是我已經寫了一段時間沒有寫入文本文件。我試圖重命名一個文件真的任何文件,我把它放到拖動框&,但然後C#說它找不到指定的文件。C#:重命名DragAndDrop列表框控件中的文件

我上傳我的項目的Dropbox這樣的人誰願意幫助並不需要從頭複製它: https://www.dropbox.com/s/9cta5dsrzosk81t/DragDropForm.v12.suo?dl=0

但這裏是我的代碼無論如何,如果它更容易爲人們回答我的問題。謝謝。

public Form1() 
    { 
     InitializeComponent(); 
    } 


    private string getFileName(string path) 
    {    
     return Path.GetFileName(path); 
    } 

    private string getDirectoryName(string path) 
    { 
     return Path.GetDirectoryName(path); 
    } 


    private void Form1_DragDrop(object sender, DragEventArgs e) 
    { 
     //TAKE dropped items and store in array. 
     string[] dropppedFiles = (string[])e.Data.GetData(DataFormats.FileDrop); 

     //LOOP through all droppped items and display them 
     foreach (string file in dropppedFiles) 
     { 
      string filename = getFileName(file);   
      listBox1.Items.Add(filename); 
      FileInfo fi = new FileInfo(filename); 
      { 
       //IF filename "NewName" doesn't exist in drag drop box. 
       if (!File.Exists("NewName")) 
       {       
        getDirectoryName(filename); 
        fi.MoveTo("NewName"); 
       } 
      } 
     } 
    } 

    private void Form1_DragEnter(object sender, DragEventArgs e) 
    { 
     if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true) 
     { 
      e.Effect = DragDropEffects.All; 
     } 
    } 

回答

0

我認爲這個問題是在這一行:

FileInfo fi = new FileInfo(filename); 

你必須通過file(完整路徑)來FileInfo這樣的:

FileInfo fi = new FileInfo(file); 

同樣會適用於getDirectoryName(filename),應該是getDirectoryName(file);,雖然你沒有使用該方法返回任何值...