我对.net编程中的托管和非托管资源感到困惑。我正在开发一个vb.net应用程序。我在微软网站上看到,如果我们使用托管资源,垃圾收集器将处理它,如果我们使用非托管资源,我们需要调用dispose。但我无法在任何地方得到以下问题的答案。
答案 0 :(得分:1)
IDisposable
其非托管或包含一些非托管IDisposable
,但.net具有良好的GC,除非您以某种方式锁定非托管res。你应该没事。答案 1 :(得分:0)
var credentials = SdkContext.AzureCredentialsFactory
.FromServicePrincipal(clientId, clientSecret, tenantId,
AzureEnvironment.AzureGlobalCloud);
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithSubscription(subscriptionID);
List<IStorageAccount> storageAccounts =
azure.StorageAccounts.List().ToList();
foreach (IStorageAccount storageaccount in storageAccounts)
{
var exportSecrets = true;
CloudStorageAccount storageAccountss = new CloudStorageAccount(
new StorageCredentials(storageaccount.Name,
storageaccount.GetKeys().FirstOrDefault().Value), true);
var connString = storageAccountss.ToString(exportSecrets);
var storageAccount = CloudStorageAccount.Parse(connString);
CloudBlobClient myClient = storageAccount.CreateCloudBlobClient();
ContainerResultSegment resultSegment = null;
BlobContinuationToken continuationToken = null;
resultSegment = myClient.ListContainersSegmented("",
ContainerListingDetails.Metadata, 5, continuationToken, null,
null);
// Enumerate the containers returned.
foreach (var container in resultSegment.Results)
{
var containers = myClient.GetContainerReference(container.Name);
foreach (IListBlobItem item in container.ListBlobs(null, false,
BlobListingDetails.None))
{
if (item.GetType() == typeof(CloudPageBlob))
{
CloudPageBlob pageBlob = (CloudPageBlob)item;
if (pageBlob.Name.Contains(".vhd"))
{
// 0. Unspecified 1. Locked 2. UnLocked
if (Convert.ToInt32(pageBlob.Properties.LeaseStatus)
== 2)
{
list.Add(new Disks { Name =
pageBlob.Name.ToString(), URI =
pageBlob.Uri.AbsoluteUri.ToString(),
ContainerName = container.Name.ToString() });
}
}
}
}
}
}
Helper helper = new Helper();
StringBuilder sb = new StringBuilder();
DataTable dt = helper.GetData(list);
foreach (DataRow dr in dt.Rows)
{
foreach (DataColumn dc in dt.Columns)
sb.Append(FormatCSV(dr[dc.ColumnName].ToString()) + ",");
sb.Remove(sb.Length - 1, 1);
sb.AppendLine();
}
File.WriteAllText("D:\\UnmanagedDisks.csv", sb.ToString());