public void RenameFile(string path, string newPath) { var files = _bucket.Find(Builders <GridFSFileInfo> .Filter.Empty).ToList(); var fileResult = files.FirstOrDefault(x => x.Filename == path); if (fileResult != null) { _bucket.Rename(fileResult.Id, newPath); } }
/// <summary> /// Renaming all revisions of a file /// To rename all revisions of a file you first use the Find or FindAsync method to find all the revisions, /// and then loop over the revisions and use the Rename or RenameAsync method to rename each revision one at a time. /// </summary> /// <param name="id"></param> /// <param name="newFilename"></param> public void RenamingAllFiles(string oldFilename, string newFilename) { //For example, to find the newest revision of the file named “securityvideo” uploaded in January 2015: IGridFSBucket bucket = GetGridFSBucket(); //string oldFilename; //string newFilename; var filter = Builders <GridFSFileInfo> .Filter.Eq(x => x.Filename, oldFilename); var filesCursor = bucket.Find(filter); var files = filesCursor.ToList(); foreach (var file in files) { bucket.Rename(file.Id, newFilename); } //var filesCursor = await bucket.FindAsync(filter); //var files = await filesCursor.ToListAsync(); //foreach (var file in files) //{ // await bucket.RenameAsync(file.Id, newFilename); //} }
/// <summary> /// Use the Rename or RenameAsync methods to rename a single file identified by its Id. /// </summary> public void RenamingFiles(ObjectId id, string newFilename) { //For example, to find the newest revision of the file named “securityvideo” uploaded in January 2015: IGridFSBucket bucket = GetGridFSBucket(); bucket.Rename(id, newFilename); //await bucket.RenameAsync(id, newFilename); }