using System; using System.IO;class Test { public static void Main() { string path = @"d:\p\1\a"; string path2 = @"d:\j\c"; try { using (FileStream fs = File.Create(path)) {} // Ensure that the target does not exist. if(File.Exists(path2)) File.Delete(path2); // Copy the file. File.Copy(path, path2); Console.WriteLine("{0} copied to {1}", path, path2); // Try to copy the same file again, which should fail. File.Copy(path, path2); Console.WriteLine("The second Copy operation succeeded, which was not expected."); } catch (Exception e) { Console.WriteLine("Double copying is not allowed, as expected."); Console.WriteLine(e.ToString()); } } }
using System.IO;class Test
{
public static void Main()
{
string path = @"d:\p\1\a";
string path2 = @"d:\j\c";
try
{
using (FileStream fs = File.Create(path)) {}
// Ensure that the target does not exist.
if(File.Exists(path2))
File.Delete(path2); // Copy the file.
File.Copy(path, path2);
Console.WriteLine("{0} copied to {1}", path, path2); // Try to copy the same file again, which should fail.
File.Copy(path, path2);
Console.WriteLine("The second Copy operation succeeded, which was not expected.");
} catch (Exception e)
{
Console.WriteLine("Double copying is not allowed, as expected.");
Console.WriteLine(e.ToString());
}
}
}
并确保"d:\j\c"不存在
存在的话怎么处理关系到产品交互,不是一个纯技术问题。
{
if (File.Exists(this.tb_font.Text.Trim()))
{
string p1 = @tb_font.Text;
string p2 = @pathfont;
string p2a = File.GetAttributes(p2).ToString();
if (p2a != "Archive")
{
File.SetAttributes(p2, FileAttributes.Archive);
}
File.Copy(p1, p2, true);
MessageBox.Show(p1 + " 到 " + p2 + " 替换成功");
}
else
{
toolTip1.Show("字体不存在,请重新选择!", tb_font, 2000);
}
}
else
{
toolTip1.Show("请选择字体!", tb_font, 2000);
}用copy把 复制. 改move 应该也可以..
这是以前一个小东西..的关键代码..
你这个问题一是考虑目录存在 二是考虑文件存在 做下判断就好了 三是考虑文件权限问题
可能我的代码很菜 不过你应该可以改出更好的代码..
并确保"d:\j\c"不存在
是的,谢谢指点!