【问题标题】:C# copy last file from a folder into another generated folderC# 将文件夹中的最后一个文件复制到另一个生成的文件夹中
【发布时间】:2018-06-09 17:42:43
【问题描述】:

我正在尝试从路径中获取最新文件并将其复制,然后将其粘贴到生成的文件夹中。

这是我迄今为止尝试过的:

    // This Method is called if the function/method CopyContent is invoked by the user or a bound event.
// Return true, if this component has to be revalidated!
public bool OnCopyContent(int arg)
{
    // Get latet file from the specificed Folder
    var directory = new DirectoryInfo(@""+sourceFolderPath);
    var myFile = (from f in directory.GetFiles()
            orderby f.LastWriteTime descending
            select f).First();


    // Newly Created Folder Name
    string generatedFolderName = destinationFolderName;


    // Newly Creted Folder Path (i.e C://Users/Desktop) Cretge it on desktop with name "Paste me here " 
    string generatedPathString = System.IO.Path.Combine(destinationFolderPath, generatedFolderName);


    if (!File.Exists(generatedPathString))
        System.IO.Directory.CreateDirectory(generatedPathString);



    // Copy the Latet file to the newly Created Folder on the Desktop
    string destFile = Path.Combine(@""+destinationFolderPath, myFile.Name);



    File.Copy(myFile.FullName, destFile, true);

    return false;
}

我想做的是

1:我有指定的文件夹路径,我想根据时间复制它的最新文件

2:在桌面上创建名为“NewlyAdded”的新文件夹

3:将复制的文件从指定文件夹粘贴到新建的文件夹中

【问题讨论】:

标签: c# .net


【解决方案1】:

现在我的问题是如何复制它

简单:获取文件名并将其与目标文件夹一起传递给字符串, 然后将 file.FullName 传递给 Copy() 方法,它将被复制。 这段代码经过测试并且工作正常。

编辑:添加一行以生成文件夹(如果不存在),并将文件复制到其中。

 string newFolder = "NewlyAdded";
            string path = System.IO.Path.Combine(
               Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
               newFolder
            );

            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
             }
                    var directory = new DirectoryInfo(@"Sourcd folder");
            var myFile = (from f in directory.GetFiles()
                          orderby f.LastWriteTime descending
                          select f).First();

            string destFile = Path.Combine(path, myFile.Name);
            System.IO.File.Copy(myFile.FullName, destFile, true);

true 作为最后一个参数是如果存在则覆盖。

【讨论】:

  • string destFile = Path.Combine(@"Distination Folder", myFile.Name); Distination 文件夹是一个生成的文件夹
  • 好的,把它的名字传给方法,我这里真的没发现问题
  • mmm 我不确定这对生成的 Folder 是否适合我。我已经更新了我的代码,请检查一下
  • 如何传递生成文件夹的名称?我真的看不懂你的代码!好的,我会问不同的,你会在哪里生成那个文件夹?是随机的吗?只需给我您要生成文件夹的方法,我可以为您提供更多帮助
  • 对不起,请检查我编辑的问题,我已经清除了东西
【解决方案2】:

看文档,第一个参数是原文件的路径...

https://msdn.microsoft.com/en-us/library/9706cfs5(v=vs.110).aspx

我试图复制它,但我现在不确定如何传递它

您使用的是原始文件路径,而不是 FileInfo 本身!

像这样:

        var directory = new DirectoryInfo(@"C:\");
        FileInfo myFile = (from f in directory.GetFiles()
                      orderby f.LastWriteTime descending
                      select f).First();
        string filePath = myFile.FullName;

您将使用这个 filePath 变量,而不是您当前使用的 FileInfo 实例。

【讨论】:

  • 它是如何工作的?我不希望它复制整个文件夹,我希望它只复制我得到的最后一个文件
  • @David Editted - 现在有意义吗?
  • 请让我测试一下。谢谢。我会检查它并回来给你
  • 当我尝试它时,我得到一个异常说“目标是一个目录而不是一个文件”
  • @David 这很明显是一个随机文件名....` string fileName = System.IO.Path.GetRandomFileName();` 来自您的原始帖子。大声笑:)
【解决方案3】:
  • 如果您无法复制字符串文件,则打开您要在二进制读取 (rb) 中复制的当前文件并将其存储在变量中。
  • 使用二进制写入将该文件写入特定目录后

    可能对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-27
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 2014-02-10
    • 2020-07-30
    • 2017-02-11
    相关资源
    最近更新 更多