Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何复制粘贴已存在的文件?_C# - Fatal编程技术网

C# 如何复制粘贴已存在的文件?

C# 如何复制粘贴已存在的文件?,c#,C#,我使用这行简单的代码复制粘贴文件: File.Copy(filename, temp_file); 现在,如果一个文件已经存在,我想在复制到某个保证不存在于目标文件夹中的名称之前对其进行重命名,方法是添加一些名称扩展名,如“copy1”“copy2”……与windows在通过资源管理器进行复制粘贴时所做的相同。如何以编程方式执行此操作?使用File.Exists方法检查文件是否存在 要重命名文件,您可以尝试创建更多循环,以检查是否存在copy(1) (File.Exists(fileName)

我使用这行简单的代码复制粘贴文件:

File.Copy(filename, temp_file);

现在,如果一个文件已经存在,我想在复制到某个保证不存在于目标文件夹中的名称之前对其进行重命名,方法是添加一些名称扩展名,如“copy1”“copy2”……与windows在通过资源管理器进行复制粘贴时所做的相同。如何以编程方式执行此操作?

使用
File.Exists
方法检查文件是否存在

要重命名文件,您可以尝试创建更多循环,以检查是否存在
copy(1)

(File.Exists(fileName) ? "File exists." : "File does not exist.")
int p = 0;
while (File.Exists(temp_file))
{
    temp_file = Path.GetTempPath() + @"temp_presentation" + p.ToString() + ".pptx";
    p++;
}
File.Copy(filename, temp_file);

您可以通过检查现有文件并为目标生成新名称,直到目标未被占用为止,如下所示:

public static IEnumerable<string> FallbackPaths(string path)
{
    yield return path;

    var dir = Path.GetDirectoryName(path);
    var file = Path.GetFileNameWithoutExtension(path);
    var ext = Path.GetExtension(path);

    yield return Path.Combine(dir, file + " - Copy" + ext);
    for (var i = 2; ; i++)
    {
        yield return Path.Combine(dir, file + " - Copy " + i + ext);
    }
}
public static void SafeCopy(string src, string dest)
{
    foreach (var path in FallbackPaths(dest))
    {
        if (!File.Exists(path))
        {
            File.Copy(src, path);
            break;
        }
    }
}
公共静态IEnumerable后备路径(字符串路径)
{
收益返回路径;
var dir=Path.GetDirectoryName(路径);
var file=Path.GetFileNameWithoutExtension(Path);
var ext=Path.GetExtension(Path);
生成返回路径.Combine(dir,file+“-Copy”+ext);
对于(变量i=2;;i++)
{
产生返回路径.Combine(dir,file+“-Copy”+i+ext);
}
}
公共静态无效安全副本(字符串src、字符串dest)
{
foreach(后备路径(dest)中的var路径)
{
如果(!File.Exists(path))
{
Copy(src,path);
打破
}
}
}
请注意,如果同时写入同名文件,则此函数可以为您提供
IOException
(由于文件已存在的原因)。

类似于:

    private static void MoveCopy(String source, String target) {
      // assuming that target directory exists

      if (!File.Exists(target))
        File.Copy(source, target);
      else
        for (int i = 1; ; ++i) {
          String name = Path.Combine(
            Path.GetDirectoryName(target),
            Path.GetFileNameWithoutExtension(target) + String.Format("(copy{0})", i) +
            Path.GetExtension(target));

          if (!File.Exists(name)) {
            File.Copy(source, name);

            break;
          }
        }
    }

...

  MoveCopy(filename, temp_file);

您想重命名现有文件还是新文件?@juharr:谢谢!我真的应该加上
++I