//找出所有 .csproj 檔案 var allFiles = Directory.GetFiles(arg, "*.csproj", SearchOption.AllDirectories); //依序以 XDocument 載入並解析出第三方套件的名稱與版本號 foreach (var file in allFiles) { var projDefinition = XDocument.Load(file); var elements = projDefinition.Element("Project")?.Element("ItemGroup")?.Elements("PackageReference");
if (elements == null) continue; if (!elements.Any()) continue; var pkgRefs = elements.ToDictionary(x => x.Attribute("Include")?.Value, x => x.Attribute("Version")?.Value); }
剩下的功能就是查詢 Nuget 取得版本號碼。 由於大部分人的使用都是直接使用 IDE 或 VS 的 package management console 來安裝,幾乎沒有使用 api 去呼叫的需求,這類型的資訊在網路上很少;若是哪天要整理的內容變很多時,手動就感覺特別的沒有效率。 我絕對不會承認官方文件看好久還是看不懂
var client = new MongoClient(MongoAddress); var collection = client.GetDatabase(HelloDatabase).GetCollection<UserEntity>(UserCollection);
Create
剛開始肯定沒有資料的,所以我們會先建立幾筆資料做為測試
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//建立一筆資料 privatestaticasyncvoidCreateUser() { await GetUsersCollection().InsertOneAsync( new UserEntity{ Name = "Shaka", Country = "Unknown", City = "Zodiac Temple"} ); } //一次建立多筆資料 privatestaticasyncvoidCreateMultiUsers() { await GetUsersCollection().InsertManyAsync(new List<UserEntity> { new UserEntity{ Name = "Jack", Country = "Taiwan", City = "Taipei"}, new UserEntity{ Name = "Sion", Country = "Unknown", City = "Underworld"}, new UserEntity{ Name = "Saga", Country = "Unknown", City = "Zodiac Temple"} } ); }