Showing posts with label code sample. Show all posts
Showing posts with label code sample. Show all posts

Tuesday, August 25, 2020

Code: Display Net Use default results with C#

 Win32_NetworkConnection


Summary

This was the WMI OS CIM class needed to get the results from a simple Windows command prompt "net use" command. 



Thoughts

This was somehow amazingly hard to find on google. For whatever reason, the only command I can find were for Win32_OperatingSystem or Win32_Share.

There is plenty of information on all the elements but just could not find a connection between net use and Win32_NetworkConnection. 


My Sample Code

                string serverName = @"\\serverName";
                string WMIClassName = "Win32_NetworkConnection";

                List<string> shares = new List<string>();

                ConnectionOptions connectionOptions = new ConnectionOptions();

                ManagementScope scope = new ManagementScope(serverName,
                                                            connectionOptions);
                scope.Connect();

                ManagementObjectSearcher worker = new ManagementObjectSearcher(scope,
                                   new ObjectQuery($"select Name from {WMIClassName}"));

                foreach (ManagementObject share in worker.Get())
                {
                    shares.Add(share["Name"].ToString());
                }


References

Friday, September 12, 2014

Blogger's Ads Location Change after Implementing Google+ Comments

For a while, I was wondering why all of a sudden my CPC disappeared. Especially with a sudden increase in traffic, I should have statistically maintained approximately the same rate. I was only getting perhaps one click to two clicks per month so I thought maybe it was a fluke.

Then I realized that in the single post view, the ad's location changed from middle of the page between comments and post to the very end of the page (after the comments). In addition, the Google+ style comments take up more space than the default blogger comments thus pushing the ads further down.

Then I started looking into how to move the ad back to being above the comments. This actually took a little longer than I expected. The code needed is in Template > Edit HTML, not under Layout. In the Edit HTML mode, there were a lot of tags to filter through so I searched for ad. After going through a few, I found this block for posts (e.g. <!-- posts -->).

Took me a couple minutes to wrap what is going on here. I saw a section that would be near the location of where the ads were (i.e. near the bottom). I thought about moving this block, <b:if cond='data:post.includeAd'>. Then noticed it has some end statements.

Seeing <data:adCode/>, I guessed this was the specific line for the ad. I moved this up between posts and comments. I included additional parameters to mimic the original location:
        <b:if cond='data:post.includeAd'>
          <div class='inline-ad'>
            <data:adCode/>
          </div>
        </b:if>

I am not exactly sure what <data:adStart/> is supposed to end so I left that in its original location. It is odd because adEnd is above that line and when it is not the first post. Either case, I tried the code at this point and seems to have moved it to where I want it to be. Now I just have to wait and see if the clicks return to know if it worked completely.

Here is my hacked up code block I eventually ended up with:

    <!-- posts -->
    <div class='blog-posts hfeed'>

      <b:include data='top' name='status-message'/>

      <data:defaultAdStart/>
      <b:loop values='data:posts' var='post'>
        <b:if cond='data:post.isDateStart'>
          <b:if cond='data:post.isFirstPost == &quot;false&quot;'>
            &lt;/div&gt;&lt;/div&gt;
          </b:if>
        </b:if>
        <b:if cond='data:post.isDateStart'>
          &lt;div class=&quot;date-outer&quot;&gt;
        </b:if>
        <b:if cond='data:post.dateHeader'>
          <h2 class='date-header'><span><data:post.dateHeader/></span></h2>
        </b:if>
        <b:if cond='data:post.isDateStart'>
          &lt;div class=&quot;date-posts&quot;&gt;
        </b:if>
        <div class='post-outer'>
        <b:include data='post' name='post'/>
        <b:if cond='data:post.includeAd'>
          <div class='inline-ad'>
            <data:adCode/>
          </div>
        </b:if>
        <b:if cond='data:blog.pageType == &quot;static_page&quot;'>
          <b:include data='post' name='comment_picker'/>
        </b:if>
        <b:if cond='data:blog.pageType == &quot;item&quot;'>
          <b:include data='post' name='comment_picker'/>
        </b:if>
        </div>
        <b:if cond='data:post.includeAd'>
          <b:if cond='data:post.isFirstPost'>
            <data:defaultAdEnd/>
          <b:else/>
            <data:adEnd/>
          </b:if>
          <data:adStart/>
        </b:if>
      </b:loop>
      <b:if cond='data:numPosts != 0'>
        &lt;/div&gt;&lt;/div&gt;
      </b:if>
      <data:adEnd/>
    </div>

Friday, May 2, 2014

Coding: Creating a Counter (MS SQL, C#) Samples

I probably create one of these for every one of my larger projects. I always have a need to track a counter of some sort.

My Standard Counter Table

USE [dbName]
GO

/****** Object:  Table [dbo].[__Counters]    Script Date: 05/01/2014 23:45:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[__Counters](
[CounterName] [varchar](50) NOT NULL,
[NextId] [int] NOT NULL,
[Notes] [varchar](max) NULL,
 CONSTRAINT [PK___Counters] PRIMARY KEY CLUSTERED
(
[CounterName] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


My Standard Stored Procedure

USE [dbName]
GO

/****** Object:  StoredProcedure [dbo].[GetCounter]    Script Date: 05/01/2014 23:45:52 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author: Douglas Lee
-- Create date: 5/1/2014
-- Description: Get next id and update for next use
-- =============================================
CREATE PROCEDURE [dbo].[GetCounter]
-- Add the parameters for the stored procedure here
@CounterName varchar(50),
@NextId int OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

    -- Insert statements for procedure here
--DECLARE @NextId int;
SET @NextId = 1;
SELECT @NextId = NextId FROM __Counters WHERE CounterName = @CounterName;
Update __Counters SET NextId = NextId + 1 WHERE CounterName = @CounterName;
RETURN
END

GO

My Standard C# Code

        public static int GetCounter(string CounterName)
        {
            SqlConnection conn = null;
            //SqlDataReader rdr = null;

            conn = new SqlConnection(c);
            conn.Open();

            // 1.  create a command object identifying the stored procedure
            SqlCommand cmd = new SqlCommand("GetCounter", conn);

            // 2. set the command object so it knows to execute a stored procedure
            cmd.CommandType = CommandType.StoredProcedure;

            // 3. add parameter to command, which will be passed to the stored procedure
            SqlParameter output = new SqlParameter("@NextId", SqlDbType.Int);
            output.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(new SqlParameter("@CounterName", CounterName));
            cmd.Parameters.Add(output);

            cmd.ExecuteNonQuery();
            int retInt = (int)cmd.Parameters["@NextId"].Value;

            conn.Close();
            
            return retInt;
        }