<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>My Distributed Life &#187; Programming</title>
	<atom:link href="http://mydistributedlife.com/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://mydistributedlife.com</link>
	<description>Musings of a Geek/Husband/Father</description>
	<lastBuildDate>Thu, 30 Apr 2015 05:11:59 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.1</generator>
	<item>
		<title>File Size to String in JavaScript</title>
		<link>http://mydistributedlife.com/2012/03/file-size-to-string-in-javascript/</link>
		<comments>http://mydistributedlife.com/2012/03/file-size-to-string-in-javascript/#comments</comments>
		<pubDate>Fri, 23 Mar 2012 05:14:35 +0000</pubDate>
		<dc:creator><![CDATA[Garett]]></dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://mydistributedlife.com/?p=21</guid>
		<description><![CDATA[Recently, I needed to figure out a way to convert the size of a file, which is being returned in bytes, to a string representation.  So, given a file size in bytes like 349792 the string “342KB&#8221; would be returned.  Inspired by the following StackOverflow post, I came up with this JavaScript version. function fileSizeToString(size) ... <span class="more"><a class="more-link" href="http://mydistributedlife.com/2012/03/file-size-to-string-in-javascript/">[Read more...]</a></span>]]></description>
				<content:encoded><![CDATA[<p>Recently, I needed to figure out a way to convert the size of a file, which is being returned in bytes, to a string representation.  So, given a file size in bytes like 349792 the string “342KB&#8221; would be returned.  Inspired by the following <a href="http://stackoverflow.com/questions/281640/how-do-i-get-a-human-readable-file-size-using-net" target="_blank">StackOverflow post</a>, I came up with this JavaScript version.</p>
<pre class="brush: javascript auto-links: false;">function fileSizeToString(size) {
    var suffix = ["B", "KB", "MB", "GB"];

    var place = Math.floor(Math.log(size) / Math.log(1024));
    var fileSize = Math.round(size / Math.pow(1024, place));

    return (fileSize + suffix[place]);
};</pre>
<p>As you can see this is a relatively simple function, and really boils down to two lines of code. The first line</p>
<pre class="brush: javascript auto-links: false;">var place = Math.floor(Math.log(size) / Math.log(1024));</pre>
<p>determines which suffix will be used. While the second line</p>
<pre class="brush: javascript auto-links: false;">var fileSize = Math.round(size / Math.pow(1024, place));</pre>
<p>calculates the file size, which is typically <a href="http://en.wikipedia.org/wiki/File_size" target="_blank">measured</a> in units of 1024.</p>
<p>In my case I only needed to support up to gigabyte file sizes, but the function could easily be modified to support larger sizes.</p>
<p>Happy Coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://mydistributedlife.com/2012/03/file-size-to-string-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Retrieving a Windows Service&#8217;s Key Name Using It&#8217;s Process ID</title>
		<link>http://mydistributedlife.com/2009/05/retrieving-a-windows-services-key-name-using-its-process-id/</link>
		<comments>http://mydistributedlife.com/2009/05/retrieving-a-windows-services-key-name-using-its-process-id/#comments</comments>
		<pubDate>Sat, 09 May 2009 05:40:23 +0000</pubDate>
		<dc:creator><![CDATA[Garett]]></dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://mydistributedlife.com/?p=6</guid>
		<description><![CDATA[I was recently asked by a friend how to retrieve the name of a running windows service from the Service Control Manager (SCM) database. He wanted to do this from .NET, using the process id of the service. I started to think of different ways that this could be accomplished and came up with a ... <span class="more"><a class="more-link" href="http://mydistributedlife.com/2009/05/retrieving-a-windows-services-key-name-using-its-process-id/">[Read more...]</a></span>]]></description>
				<content:encoded><![CDATA[<p>I was recently asked by a friend how to retrieve the name of a running windows service from the Service Control Manager (SCM) database. He wanted to do this from .NET, using the process id of the service. I started to think of different ways that this could be accomplished and came up with a couple, utilizing <a href="http://msdn.microsoft.com/en-us/library/ms257340%28VS.80%29.aspx">WMI.NET</a>. Both approaches are similar, with a slight variation in the second.</p>
<h3><strong>First Solution</strong></h3>
<p>This function gets the service&#8217;s name by retrieving all services registered with the SCM, and comparing their process ids to the one supplied. </p>
<pre class="brush: csharp; auto-links: false;">public static string GetServiceNameByProcessId(uint processId)
{
    string serviceName = String.Empty;

    ManagementClass mc = new ManagementClass("Win32_Service");

    foreach (ManagementObject service in mc.GetInstances())
    {
        if ((uint)service["ProcessId"] == processId)
        {
            serviceName = (string)service["Name"];
            break;
        }
    }

    return serviceName;
}</pre>
<p>&nbsp;&nbsp; </p>
<h3><strong>Second Solution</strong></h3>
<p>This function uses <a href="http://msdn.microsoft.com/en-us/library/aa394606%28VS.85%29.aspx">WQL</a> to query for&nbsp; the specific service instance, using the process id in the WHERE clause. This avoids having to iterate over all the services within the SCM.</p>
<pre class="brush: csharp; auto-links: false;">public static string GetServiceNameByProcessId(uint processId)
{
    string serviceName = String.Empty;

    SelectQuery query = new SelectQuery("Win32_Service", String.Format("ProcessId={0}", processId));
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);

    foreach (ManagementObject service in searcher.Get())
    {
        if ((uint)service["ProcessId"] == processId)
        {
            serviceName = (string)service["Name"];
            break;
        }
    }
   
    return serviceName;
}&nbsp; </pre>
<p>So, there you have it. These are the approaches that I came up with. In the future, I may present another that uses the Windows API, in particular the <a href="http://msdn.microsoft.com/en-us/library/ms682640%28VS.85%29.aspx">EnumServicesStatusEx</a> function, to accomplish the same task. Can anyone think of other approaches or suggest changes to the ones I&#8217;ve presented?</p>
]]></content:encoded>
			<wfw:commentRss>http://mydistributedlife.com/2009/05/retrieving-a-windows-services-key-name-using-its-process-id/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
