2016-06-21 52 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);,虽然你没有使用该方法返回任何值...