简体   繁体   English

C#将最后一个文件从一个文件夹复制到另一个生成的文件夹

[英]C# copy last file from a folder into another generated folder

I'm trying to get a latest file from a path and copying it, then paste it into a generated folder. 我正在尝试从路径中获取最新文件并将其复制,然后将其粘贴到生成的文件夹中。

This is what I tried so far: 这是我到目前为止尝试过的:

    // 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;
}

What im trying to do is 我想做的是

1 : I have Specifed Folder Path , i want to copy it's latest file in it depending on timee 1:我有指定的文件夹路径,我想根据时间在其中复制它的最新文件

2: Create new Folder on Desktop with name " NewlyAdded" 2:在桌面上创建名为“ NewlyAdded”的新文件夹

3: Paste the Copied File from the Specifed Folder to the Newly Created Folder 3:将复制的文件从指定的文件夹粘贴到新创建的文件夹

now my issue is how to copy it 现在我的问题是如何复制它

simply : take the file name ans pass it with the destination folder to a string, then pass the file.FullName to Copy() method and it'll be copied. 简单:将文件名与目标文件夹一起传递给字符串,然后将文件全名传递给Copy()方法,它将被复制。 This code tested and worked. 此代码经过测试并有效。

EDIT : Added a line to generate folder if not exist, and copy the file to it. 编辑:添加一行以生成文件夹(如果不存在),并将文件复制到其中。

 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);

the true as a last parameter is to overwrite if exist. 最后一个参数true是覆盖(如果存在)。

Looking at the documentation, the first parameter is the path of the original file... 查看文档,第一个参数是原始文件的路径。

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

I have trying to copy it however im not sure now how to pass it in 我正在尝试复制它,但是我现在不确定如何传递

You use the file path of the original, not the FileInfo itself! 您使用原始文件的文件路径,而不是FileInfo本身!

Like this: 像这样:

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

You'd use this filePath variable, not the FileInfo instance you're currently using. 您将使用此filePath变量,而不是当前使用的FileInfo实例。

  • if you coudn't copy string file then open your current file which you want to copy in binary read (rb) and store that in variable. 如果您不能复制字符串文件,则以二进制读取(rb)打开要复制的当前文件,并将其存储在变量中。
  • after write that file in particular directory by using binary write 通过使用二进制写入将文件写入特定目录后

    may be it should help you 可能对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM