1 使用MongoDB.Driverと、 2 使用MongoDB.Driver.GridFS。 3 使用System.IOと、 4 5 名前空間Wisdombud.Mongo 6 { 7 /// <要約> 8 /// 9 /// </要約> 10の パブリック クラスMongoFileBll 11 { 12 プライベートMongoDatabaseリポジトリ。 13 /// <要約> 14 /// 15 /// </要約> 16 /// <戻る> </戻る> 17 公共はMongoCursor <MongoGridFSFileInfo> のfindAll() 18 { 19 リターン この.repository.GetGridFS(MongoGridFSSettings.Defaults).FindAll(); 20 } 21 /// <要約> 22 /// 23 /// </要約> 24 /// ます。<param name = "pConnectionstring"> </ PARAM> 25 公衆 MongoFileBll(文字列pConnectionstring) 26 { 27 MongoUrl mongourl = MongoUrl.Create(pConnectionstring)。 28 のvarクライアント= 新しいですMongoClient(mongourl)。 29 MongoServerサーバ= client.GetServer()。 30 この .repository = server.GetDatabase(mongourl.DatabaseName)。 31 } 32 /// <要約> 33 /// 34 /// </要約> 35 /// ます。<param name = "filePathに"> </ PARAM> 36 公共 ボイド UploadFile(文字列filePathに) 37 { 38 のFileInfo Fiの= 新規のFileInfo(filePathに)。 39 本.repository.GetGridFS(MongoGridFSSettings.Defaults).Upload(filePathに、fi.Name)。 40 } 41 /// <要約> 42 /// 43 /// </要約> 44 /// ます。<param name = "filePathに"> </ PARAM> 45 /// <PARAM NAME = "FILENAME"> < / PARAM> 46 47 公共 ボイド UploadFile(文字列 filePathに、文字列filename)で 48 { 49 この.repository.GetGridFS(MongoGridFSSettings.Defaults).Upload(filePathには、ファイル名)。 50 } 51 52 /// <概要> 53 /// 54 /// </要約> 55 /// ます。<param name = "FILENAME"> </ PARAM> 56 /// <PARAM NAME = "filePathに"> </ PARAM> 57 公共 ボイド DownloadFile(文字列filename)で 58 { 59 この.repository.GetGridFS(MongoGridFSSettings.Defaults).Download(filename)で。 60 } 61 /// <要約> 62 /// 63 /// </要約> 64 /// ます。<param name = "FILENAME"> </ PARAM> 文字列filename)で 66 { 67 この.repository.GetGridFS(MongoGridFSSettings.Defaults).Delete(filename)で。 68 } 69 /// <要約> 70 /// 71 /// </要約> 72 公共 ボイドDeleteAllが() 73 { 74 のforeach(VAR工大で この.repository.GetGridFS(MongoGridFSSettings.Defaults).FindAll()) 75 { 76 inst.Delete()。 77 } 78 } 79 } 80 }
使用
1 MongoFileBll TL = 新しい MongoFileBll(" のMongoDB://127.0.0.1:27017 / chzhaotest " )。 2 列フォルダ= @ " D:\ Wisdombud \ 2016 \ 20160324_Wisdombud.Mongo \ソースコード\ Wisdombud.Mongo.Sample \ binに\デバッグ" 。 3 DirectoryInfoディ= 新しいDirectoryInfo(フォルダ)。 4 のforeach(VARのファイルにdi.GetFiles()) 5 { 6 tl.UploadFile(file.FullName、Guid.NewGuid()のToString()); 7 } 8 VAR AAA = tl.FindAll()。 9 foreachの(VAR工大でAAA) 10 { 11 Console.WriteLineを(inst.Name)。 12 }