简体   繁体   中英

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

2: Create new Folder on Desktop with name " NewlyAdded"

3: Paste the Copied File from the Specifed Folder to the Newly Created Folder

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. 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.

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

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!

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.

  • 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.
  • after write that file in particular directory by using binary write

    may be it should help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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