Windows service and Win32_DiskQuota<p>Hi,<br>I want get the actual quota's assigned to a user account.<br>The code is as follows.<br><br>public class QuotaInformation<br>{<br>public String DiskSpaceUsed;<br>public String Limit;<br>public String QuotaVolume;<br>public String User;<br>}<br>.<br>.<br>.<br>void GetQuotaInformation()<br>{<br>ManagementObjectSearcher searcher = new ManagementObjectSearcher(@&quot;\root\cimv2&quot;,<br>&quot;SELECT DiskSpaceUsed, Limit, QuotaVolume, User FROM Win32_DiskQuota&quot;);<br><br>foreach (ManagementObject queryObj in searcher.Get())<br>{<br>Console.WriteLine(&quot;Disk Space Used: &quot; + queryObj[&quot;DiskSpaceUsed&quot;].ToString());<br>Console.WriteLine(&quot;Limit: &quot; + queryObj[&quot;Limit&quot;].ToString());<br>Console.WriteLine(&quot;QuotaVolume:&quot; + queryObj[&quot;QuotaVolume&quot;].ToString());<br>Console.WriteLine(&quot;User: &quot; + queryObj[&quot;User&quot;].ToString());<br><br>QuotaInformation quotaInfo = new QuotaInformation();<br>quotaInfo.DiskSpaceUsed = queryObj[&quot;DiskSpaceUsed&quot;].ToString();<br>quotaInfo.Limit = queryObj[&quot;Limit&quot;].ToString();<br>quotaInfo.QuotaVolume = queryObj[&quot;QuotaVolume&quot;].ToString();<br>quotaInfo.User = queryObj[&quot;User&quot;].ToString();<br><br>}<br>}<br><br>when i used the code in the windows application. it works. it gets the actual quota <br><br>inforamation for each user account on my computer.<br>However, i used the code in the windows service. it gets quota information only for <br><br>Asministrater. not for the other users (for example, dad, mam, and grampa). <br>How I can get the quota information for all the users on my computer. <br><br>Thanks for reading.<br></p>© 2009 Microsoft Corporation. All rights reserved.Thu, 19 Jun 2008 00:15:52 Z57c2550b-52b8-4245-afcf-6893884c0006http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/57c2550b-52b8-4245-afcf-6893884c0006#57c2550b-52b8-4245-afcf-6893884c0006http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/57c2550b-52b8-4245-afcf-6893884c0006#57c2550b-52b8-4245-afcf-6893884c0006hedwighttp://social.msdn.microsoft.com/Profile/en-US/?user=hedwigWindows service and Win32_DiskQuota<p>Hi,<br>I want get the actual quota's assigned to a user account.<br>The code is as follows.<br><br>public class QuotaInformation<br>{<br>public String DiskSpaceUsed;<br>public String Limit;<br>public String QuotaVolume;<br>public String User;<br>}<br>.<br>.<br>.<br>void GetQuotaInformation()<br>{<br>ManagementObjectSearcher searcher = new ManagementObjectSearcher(@&quot;\root\cimv2&quot;,<br>&quot;SELECT DiskSpaceUsed, Limit, QuotaVolume, User FROM Win32_DiskQuota&quot;);<br><br>foreach (ManagementObject queryObj in searcher.Get())<br>{<br>Console.WriteLine(&quot;Disk Space Used: &quot; + queryObj[&quot;DiskSpaceUsed&quot;].ToString());<br>Console.WriteLine(&quot;Limit: &quot; + queryObj[&quot;Limit&quot;].ToString());<br>Console.WriteLine(&quot;QuotaVolume:&quot; + queryObj[&quot;QuotaVolume&quot;].ToString());<br>Console.WriteLine(&quot;User: &quot; + queryObj[&quot;User&quot;].ToString());<br><br>QuotaInformation quotaInfo = new QuotaInformation();<br>quotaInfo.DiskSpaceUsed = queryObj[&quot;DiskSpaceUsed&quot;].ToString();<br>quotaInfo.Limit = queryObj[&quot;Limit&quot;].ToString();<br>quotaInfo.QuotaVolume = queryObj[&quot;QuotaVolume&quot;].ToString();<br>quotaInfo.User = queryObj[&quot;User&quot;].ToString();<br><br>}<br>}<br><br>when i used the code in the windows application. it works. it gets the actual quota <br><br>inforamation for each user account on my computer.<br>However, i used the code in the windows service. it gets quota information only for <br><br>Asministrater. not for the other users (for example, dad, mam, and grampa). <br>How I can get the quota information for all the users on my computer. <br><br>Thanks for reading.<br></p>Mon, 25 Jun 2007 00:18:51 Z2007-06-28T07:59:46Zhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/57c2550b-52b8-4245-afcf-6893884c0006#a2438098-b5f5-4c89-a23e-7bc1ae52bc7bhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/57c2550b-52b8-4245-afcf-6893884c0006#a2438098-b5f5-4c89-a23e-7bc1ae52bc7bFigo Feihttp://social.msdn.microsoft.com/Profile/en-US/?user=Figo%20FeiWindows service and Win32_DiskQuota<p>Hi, </p> <p>Please make sure the service is run under Local System which is privileged.</p> <p>Thanks</p> <p> </p>Thu, 28 Jun 2007 06:12:40 Z2007-06-28T06:12:40Zhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/57c2550b-52b8-4245-afcf-6893884c0006#bc0465ba-c283-4d64-9d5d-4d0b8988edc7http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/57c2550b-52b8-4245-afcf-6893884c0006#bc0465ba-c283-4d64-9d5d-4d0b8988edc7Figo Feihttp://social.msdn.microsoft.com/Profile/en-US/?user=Figo%20FeiWindows service and Win32_DiskQuota<p>Hi, hedwig</p> <p>I tested the following code in a windows service and succeeded:</p> <p>(Set Service Account to LocalSystem instead of User to test)</p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;color:blue;font-family:新宋体"> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;color:blue;font-family:新宋体">public</span><span lang=EN-US style="font-size:9pt;font-family:新宋体"> <span style="color:blue">class</span> <span style="color:#2b91af">QuotaInformation</span></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">        {</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">            <span style="color:blue">public</span> <span style="color:#2b91af">String</span> DiskSpaceUsed;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">            <span style="color:blue">public</span> <span style="color:#2b91af">String</span> Limit;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">            <span style="color:blue">public</span> <span style="color:#2b91af">String</span> QuotaVolume;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">            <span style="color:blue">public</span> <span style="color:#2b91af">String</span> User;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">        }</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">        <span style="color:blue">string</span> str = <span style="color:blue">string</span>.Empty;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">        <span style="color:blue">void</span> GetQuotaInformation()</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">        {</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">            <span style="color:#2b91af">ManagementObjectSearcher</span> searcher = <span style="color:blue">new</span> <span style="color:#2b91af">ManagementObjectSearcher</span>(<span style="color:#a31515">@&quot;\root\cimv2&quot;</span>,</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">            <span style="color:#a31515">&quot;SELECT DiskSpaceUsed, Limit, QuotaVolume, User FROM Win32_DiskQuota&quot;</span>);</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体"> </span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">            <span style="color:blue">foreach</span> (<span style="color:#2b91af">ManagementObject</span> queryObj <span style="color:blue">in</span> searcher.Get())</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">            {</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">                str += <span style="color:#a31515">&quot;Disk Space Used: &quot;</span> + queryObj[<span style="color:#a31515">&quot;DiskSpaceUsed&quot;</span>].ToString() + <span style="color:#2b91af">Environment</span>.NewLine;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">                str += <span style="color:#a31515">&quot;Limit: &quot;</span> + queryObj[<span style="color:#a31515">&quot;Limit&quot;</span>].ToString() + <span style="color:#2b91af">Environment</span>.NewLine;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">                str += <span style="color:#a31515">&quot;QuotaVolume:&quot;</span> + queryObj[<span style="color:#a31515">&quot;QuotaVolume&quot;</span>].ToString() + <span style="color:#2b91af">Environment</span>.NewLine;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">                str += <span style="color:#a31515">&quot;User: &quot;</span> + queryObj[<span style="color:#a31515">&quot;User&quot;</span>].ToString() + <span style="color:#2b91af">Environment</span>.NewLine;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">                <span style="color:#2b91af">QuotaInformation</span> quotaInfo = <span style="color:blue">new</span> <span style="color:#2b91af">QuotaInformation</span>();</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">                quotaInfo.DiskSpaceUsed = queryObj[<span style="color:#a31515">&quot;DiskSpaceUsed&quot;</span>].ToString();</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">                quotaInfo.Limit = queryObj[<span style="color:#a31515">&quot;Limit&quot;</span>].ToString();</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">                quotaInfo.QuotaVolume = queryObj[<span style="color:#a31515">&quot;QuotaVolume&quot;</span>].ToString();</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">                quotaInfo.User = queryObj[<span style="color:#a31515">&quot;User&quot;</span>].ToString();</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">            }</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">        }</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">        <span style="color:blue">protected</span> <span style="color:blue">override</span> <span style="color:blue">void</span> OnStart(<span style="color:blue">string</span>[] args)</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">        {</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">            <span style="color:green">// TODO: Add code here to start your service.</span></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">            GetQuotaInformation();</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">            <span style="color:#2b91af">FileStream</span> fs = <span style="color:blue">new</span> <span style="color:#2b91af">FileStream</span>(<span style="color:#a31515">@&quot;D:\testService.txt&quot;</span>, <span style="color:#2b91af">FileMode</span>.OpenOrCreate, <span style="color:#2b91af">FileAccess</span>.Write);</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">            <span style="color:#2b91af">StreamWriter</span> m_streamWriter = <span style="color:blue">new</span> <span style="color:#2b91af">StreamWriter</span>(fs);</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">            m_streamWriter.BaseStream.Seek(0, <span style="color:#2b91af">SeekOrigin</span>.End);</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">            m_streamWriter.WriteLine(<span style="color:#a31515">&quot;testService:         Service Started&quot;</span> + <span style="color:#2b91af">DateTime</span>.Now.ToString() + <span style="color:#a31515">&quot;\n&quot;</span>);</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">            m_streamWriter.Write(str);</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">            m_streamWriter.Flush();</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">            m_streamWriter.Close();</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;text-align:left" align=left><span lang=EN-US style="font-size:9pt;font-family:新宋体">            fs.Close();</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt"><span lang=EN-US style="font-size:9pt;font-family:新宋体">        }</span></p></div></div> <p> </p></span> <p>Then check the testService.txt file under D: directory.</p> <p class=MsoNormal style="margin:0cm 0cm 0pt">Please give a try.</p> <p>Thanks</p>Thu, 28 Jun 2007 07:57:04 Z2007-06-28T07:59:46Z