C#新手求助:如何通过Mega API获取账户Metrics信息
Hey there! Let's work through your issue with accessing FoldersCount, FilesCount, and other metrics from the Mega API. I see you're a C# newbie just getting started with the API—no worries, we'll break this down step by step.
The Core Problem
Your custom AccountInfo struct is causing confusion here. When you map the official IAccountInformation return value to your own struct, you're not properly handling the IStorageMetrics interface implementation under the hood. Plus, you can't just output IEnumerable<IStorageMetrics> directly—it won't show the actual data, which is why you're seeing those errors when trying to display or iterate over it.
Step-by-Step Solution
1. Ditch the Custom Struct (For Now)
The MegaApiClient already returns a fully implemented IAccountInformation object that has all the data you need. No need to redefine your own struct—this avoids mismatches with the API's interface implementations.
Update your account info method to return the official type:
MegaApiClient myMegaClient = new MegaApiClient(); public void megaAccLogIn(string megaAccUserName, string megaAccPassword) { if (myMegaClient.IsLoggedIn) { return; } myMegaClient.Login(megaAccUserName, megaAccPassword); } public IAccountInformation GetAccountDetails() { megaAccLogIn(megaAccUserName, megaAccPassword); return myMegaClient.GetAccountInformation(); }
2. Properly Iterate Over Metrics
Since Metrics is an IEnumerable<IStorageMetrics>, you need to loop through each metric entry to access its properties (like FoldersCount or NodeId). Directly outputting the enumerable will just show the type name, not the data.
Update your button click handler to properly process metrics:
private void button5_Click(object sender, EventArgs e) { var accountInfo = GetAccountDetails(); // Build quota text var quotaDisplay = $"Total Quota: {FormatBytes(accountInfo.TotalQuota)}\r\n" + $"Used Quota: {FormatBytes(accountInfo.UsedQuota)}\r\n" + $"Available Quota: {FormatBytes(accountInfo.TotalQuota - accountInfo.UsedQuota)}\r\n\r\n"; // Build metrics text by iterating each entry var metricsDisplay = "Storage Metrics:\r\n"; foreach (var storageMetric in accountInfo.Metrics) { metricsDisplay += $"Node ID: {storageMetric.NodeId}\r\n" + $"Folders: {storageMetric.FoldersCount}\r\n" + $"Files: {storageMetric.FilesCount}\r\n" + $"Bytes Used: {FormatBytes(storageMetric.BytesUsed)}\r\n\r\n"; } textBox1.Text = quotaDisplay + metricsDisplay; }
3. Ensure Your FormatBytes Method Works
Just to be thorough, here's a reliable implementation of the byte formatting function if you don't already have one:
private string FormatBytes(long bytes) { string[] sizeSuffixes = { "B", "KB", "MB", "GB", "TB" }; if (bytes == 0) return "0 B"; long bytesAbs = Math.Abs(bytes); int index = Convert.ToInt32(Math.Floor(Math.Log(bytesAbs, 1024))); double value = bytesAbs / Math.Pow(1024, index); return $"{value:0.##} {sizeSuffixes[index]}"; }
Why This Works
- By using the official
IAccountInformationtype, you're leveraging the API's built-in implementation of all properties, including theMetricscollection. Your custom struct wasn't properly mirroring this, leading to null or uninitialized metric data. - Iterating over
Metricslets you access eachIStorageMetricsinstance's individual properties, which is the only way to get the folder/file counts and other details you need.
内容的提问来源于stack exchange,提问作者Eng.Mohamed Bassuny

