各位高手...小弟今天碰到一个头疼的问题..找了好多资料都没用
 我在修改一个类别名的同时需要修改相对应的文件夹的名称...我用Directory.Move(OldUrl, NewUrl);[这个好像是移动文件夹的] 它提示错误:源路径必须和目标路径不同  
请问我该怎么去重命名这文件夹呢
在此谢过了!

解决方案 »

  1.   

    System.IO.Directory.Move()没有问题
      

  2.   

    可是为什么我用Directory.Move(OldUrl, NewUrl);[这个好像是移动文件夹的] 它提示错误:源路径必须和目标路径不同呢...请您好说详细点好吗?谢谢!
      

  3.   

    原文件夹名称old,新文件夹名称new
    Directory.Move(路径\old, 路径\new);
      

  4.   


    using System;
    using System.IO;public class MoveToTest 
    {
        public static void Main() 
        {        // Make a reference to a directory.
            DirectoryInfo di = new DirectoryInfo("TempDir");        // Create the directory only if it does not already exist.
            if (di.Exists == false)
                di.Create();        // Create a subdirectory in the directory just created.
            DirectoryInfo dis = di.CreateSubdirectory("SubDir");        // Move the main directory. Note that the contents move with the directory.
            if (Directory.Exists("NewTempDir") == false)
                di.MoveTo("NewTempDir");        try 
            {
                // Attempt to delete the subdirectory. Note that because it has been
                // moved, an exception is thrown.
                dis.Delete(true);
            } 
            catch (Exception) 
            {
                // Handle this exception in some way, such as with the following code:
                // Console.WriteLine("That directory does not exist.");
            }        // Point the DirectoryInfo reference to the new directory.
            //di = new DirectoryInfo("NewTempDir");        // Delete the directory.
            //di.Delete(true);
        }
    }
      

  5.   

    我试了...还是不行...我给大家发下代码看下
    string OldUrl=Server.MapPath("~/Upfile/"+ds.Tables[0].Rows[0]["ClassName"].ToString());
                string NewUrl = Server.MapPath("~/Upfile/" + Text_ModifyClassName.Text.ToString().Trim());//修改资料下载目录
                
                Directory.Move(OldUrl, NewUrl);我是这样写的...出错!
      

  6.   

    參考如下代碼:            DirectoryInfo di = new DirectoryInfo(@"F:\kl1");
                di.MoveTo(@"F:\kl2");
                //執行後,F:\kl1變成了F:\kl2
      

  7.   

    你看一下
    Directory.Move(OldUrl, NewUrl); 
    中的OldUrl和 NewUrl是不是相同, Move方法要求这两个是不同的。
      

  8.   

    你可以使用类似如下的比较来使用Move:
    if (OldUrl!=NewUrl)
    {
        Directory.Move(OldUrl, NewUrl);  
    }
      

  9.   

    用file.move 原形: 
    public static void Move ( 
    string sourceFileName, 
    string destFileName 

    参数 
    sourceFileName 
    要移动的文件的名称。 
    destFileName 
    文件的新路径。 using System; 
    using System.IO; class Test 

    public static void Main() 

    string path = @"c:\temp\MyTest.txt"; 
    string path2 = @"c:\temp2\MyTest.txt"; 
    try 

    if (!File.Exists(path)) 

    // This statement ensures that the file is created, 
    // but the handle is not kept. 
    using (FileStream fs = File.Create(path)) {} 
    } // Ensure that the target does not exist. 
    if (File.Exists(path2)) 
    File.Delete(path2); // Move the file. 
    File.Move(path, path2); 
    Console.WriteLine("{0} was moved to {1}.", path, path2); // See if the original exists now. 
    if (File.Exists(path)) 

    Console.WriteLine("The original file still exists, which is unexpected."); 

    else 

    Console.WriteLine("The original file no longer exists, which is expected."); 
    } } 
    catch (Exception e) 

    Console.WriteLine("The process failed: {0}", e.ToString()); 


    }
      

  10.   

    代碼,衹是給個示例你參考,並不包醫百病。
    你的問題不是Move管不管用的問題,而是你想要做什么?即然你要重命名,那就應該給他傳兩個不同的目錄進去。