Exemplo n.º 1
0
 /// <summary>Retrieves the IShellFolder associated with a <see cref="KNOWNFOLDERID"/>.</summary>
 /// <param name="id">The known folder.</param>
 /// <returns>The <see cref="IShellFolder"/> instance.</returns>
 public static IShellFolder GetIShellFolder(this KNOWNFOLDERID id)
 {
     using var desktop = ComReleaserFactory.Create(new ShellDesktop() as IShellFolder);
     using var pidl    = id.PIDL();
     return(desktop.Item.BindToObject <IShellFolder>(pidl));
 }
Exemplo n.º 2
0
        //********************************************************************************************
        // Function: GetConnectionCostandDataPlanStatus
        //
        // Description: Enumerates the connections and displays the cost and data plan status for each connection
        //
        //********************************************************************************************
        static void GetConnectionCostandDataPlanStatus()
        {
            HRESULT hr    = HRESULT.S_OK;
            bool    bDone = false;
            int     numberOfConnections = 0;

            try
            {
                using var pCostManager = ComReleaserFactory.Create(new INetworkCostManager());
                var pNLM = (INetworkListManager)pCostManager.Item;
                var pNetworkConnections = pNLM.GetNetworkConnections();

                while (!bDone)
                {
                    //Get cost and data plan status info for each of the connections on the machine
                    hr = pNetworkConnections.Next(1, out var ppConnection, out var cFetched);
                    if ((HRESULT.S_OK == hr) && (cFetched > 0))
                    {
                        try
                        {
                            using var pConnection = ComReleaserFactory.Create(ppConnection);
                            numberOfConnections++;
                            var interfaceGUID = pConnection.Item.GetAdapterId();
                            Console.Write("--------------------------------------------------------------\n");
                            GetInterfaceType(interfaceGUID, hr);

                            // get the connection interface
                            var pConnectionCost = (INetworkConnectionCost)pConnection.Item;
                            var cost            = pConnectionCost.GetCost();
                            var dataPlanStatus  = pConnectionCost.GetDataPlanStatus();
                            DisplayCostDescription(cost);
                            DisplayDataPlanStatus(dataPlanStatus);

                            //to give suggestions for data usage, depending on cost and data usage.
                            CostBasedSuggestions(cost, dataPlanStatus);
                        }
                        catch (Exception ex)
                        {
                            hr = ex.HResult;
                        }
                    }
                    else
                    {
                        bDone = true;
                    }

                    if (numberOfConnections == 0)
                    {
                        Console.Write("Machine has no network connection\n");
                    }
                }
            }
            catch (Exception ex)
            {
                hr = ex.HResult;
            }

            if (hr != HRESULT.S_FALSE)
            {
                DisplayError(hr);
            }
        }