我在修改一个类别名的同时需要修改相对应的文件夹的名称...我用Directory.Move(OldUrl, NewUrl);[这个好像是移动文件夹的] 它提示错误:源路径必须和目标路径不同
请问我该怎么去重命名这文件夹呢
在此谢过了!
16 个解决方案
#1
System.IO.Directory.Move()没有问题
#2
可是为什么我用Directory.Move(OldUrl, NewUrl);[这个好像是移动文件夹的] 它提示错误:源路径必须和目标路径不同呢...请您好说详细点好吗?谢谢!
#3
原文件夹名称old,新文件夹名称new
Directory.Move(路径\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
就是使用Move来改名。
#6
我试了...还是不行...我给大家发下代码看下
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);
我是这样写的...出错!
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);
我是这样写的...出错!
#7
參考如下代碼:
DirectoryInfo di = new DirectoryInfo(@"F:\kl1");
di.MoveTo(@"F:\kl2");
//執行後,F:\kl1變成了F:\kl2
#8
你看一下
Directory.Move(OldUrl, NewUrl);
中的OldUrl和 NewUrl是不是相同, Move方法要求这两个是不同的。
Directory.Move(OldUrl, NewUrl);
中的OldUrl和 NewUrl是不是相同, Move方法要求这两个是不同的。
#9
你可以使用类似如下的比较来使用Move:
if (OldUrl!=NewUrl)
{
Directory.Move(OldUrl, NewUrl);
}
#10
路径是相同的...后面的那个文件夹不同.
我试了下7楼兄弟的...也没用
不同的是7楼兄弟指定的是绝对路径
我的用是相对路径.
我试了下7楼兄弟的...也没用
不同的是7楼兄弟指定的是绝对路径
我的用是相对路径.
#11
OK了...谢谢各位!
已经搞定了!
已经搞定了!
#12
用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());
}
}
}
原形:
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());
}
}
}
#13
代碼,衹是給個示例你參考,並不包醫百病。
你的問題不是Move管不管用的問題,而是你想要做什么?即然你要重命名,那就應該給他傳兩個不同的目錄進去。
你的問題不是Move管不管用的問題,而是你想要做什么?即然你要重命名,那就應該給他傳兩個不同的目錄進去。
#14
谢谢大家!
小弟通过你们的帮忙已经搞定了
多谢大家~!
小弟通过你们的帮忙已经搞定了
多谢大家~!
#15
真不赖啊
#16
你怎么搞定的?
#1
System.IO.Directory.Move()没有问题
#2
可是为什么我用Directory.Move(OldUrl, NewUrl);[这个好像是移动文件夹的] 它提示错误:源路径必须和目标路径不同呢...请您好说详细点好吗?谢谢!
#3
原文件夹名称old,新文件夹名称new
Directory.Move(路径\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
就是使用Move来改名。
#6
我试了...还是不行...我给大家发下代码看下
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);
我是这样写的...出错!
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);
我是这样写的...出错!
#7
參考如下代碼:
DirectoryInfo di = new DirectoryInfo(@"F:\kl1");
di.MoveTo(@"F:\kl2");
//執行後,F:\kl1變成了F:\kl2
#8
你看一下
Directory.Move(OldUrl, NewUrl);
中的OldUrl和 NewUrl是不是相同, Move方法要求这两个是不同的。
Directory.Move(OldUrl, NewUrl);
中的OldUrl和 NewUrl是不是相同, Move方法要求这两个是不同的。
#9
你可以使用类似如下的比较来使用Move:
if (OldUrl!=NewUrl)
{
Directory.Move(OldUrl, NewUrl);
}
#10
路径是相同的...后面的那个文件夹不同.
我试了下7楼兄弟的...也没用
不同的是7楼兄弟指定的是绝对路径
我的用是相对路径.
我试了下7楼兄弟的...也没用
不同的是7楼兄弟指定的是绝对路径
我的用是相对路径.
#11
OK了...谢谢各位!
已经搞定了!
已经搞定了!
#12
用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());
}
}
}
原形:
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());
}
}
}
#13
代碼,衹是給個示例你參考,並不包醫百病。
你的問題不是Move管不管用的問題,而是你想要做什么?即然你要重命名,那就應該給他傳兩個不同的目錄進去。
你的問題不是Move管不管用的問題,而是你想要做什么?即然你要重命名,那就應該給他傳兩個不同的目錄進去。
#14
谢谢大家!
小弟通过你们的帮忙已经搞定了
多谢大家~!
小弟通过你们的帮忙已经搞定了
多谢大家~!
#15
真不赖啊
#16
你怎么搞定的?