• Upgrade your Internet Experience
  • Sign in
  • Microsoft.com
  • United States (English)
    Brasil (Português)Česká republika (Čeština)Deutschland (Deutsch)España (Español)France (Français)Italia (Italiano)Россия (Русский)대한민국 (한국어)中华人民共和国 (中文)台灣 (中文)日本 (日本語)香港特别行政區 (中文)
 
 
Visual C# Developer Center
 
 
Home
 
 
Library
 
 
Learn
 
 
Downloads
 
 
Support
 
 
Community
 
 
Forums
 
 
 
Visual C# Developer Center > Visual C# Forums > Visual C# General > Windows service and Win32_DiskQuota
Ask a questionAsk a question
Search Forums:
  • Search Visual C# General Forum Search Visual C# General Forum
  • Search All Visual C# Forums Search All Visual C# Forums
  • Search All MSDN Forums Search All MSDN Forums
 

AnswerWindows service and Win32_DiskQuota

  • Monday, June 25, 2007 12:18 AMhedwig Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    Hi,
    I want get the actual quota's assigned to a user account.
    The code is as follows.

    public class QuotaInformation
    {
    public String DiskSpaceUsed;
    public String Limit;
    public String QuotaVolume;
    public String User;
    }
    .
    .
    .
    void GetQuotaInformation()
    {
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"\root\cimv2",
    "SELECT DiskSpaceUsed, Limit, QuotaVolume, User FROM Win32_DiskQuota");

    foreach (ManagementObject queryObj in searcher.Get())
    {
    Console.WriteLine("Disk Space Used: " + queryObj["DiskSpaceUsed"].ToString());
    Console.WriteLine("Limit: " + queryObj["Limit"].ToString());
    Console.WriteLine("QuotaVolume:" + queryObj["QuotaVolume"].ToString());
    Console.WriteLine("User: " + queryObj["User"].ToString());

    QuotaInformation quotaInfo = new QuotaInformation();
    quotaInfo.DiskSpaceUsed = queryObj["DiskSpaceUsed"].ToString();
    quotaInfo.Limit = queryObj["Limit"].ToString();
    quotaInfo.QuotaVolume = queryObj["QuotaVolume"].ToString();
    quotaInfo.User = queryObj["User"].ToString();

    }
    }

    when i used the code in the windows application. it works. it gets the actual quota

    inforamation for each user account on my computer.
    However, i used the code in the windows service. it gets quota information only for

    Asministrater. not for the other users (for example, dad, mam, and grampa).
    How I can get the quota information for all the users on my computer.

    Thanks for reading.

    • ReplyReply
    • QuoteQuote
     

Answers

  • Thursday, June 28, 2007 7:57 AMFigo FeiMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Vote As Helpful
    0

    Hi, hedwig

    I tested the following code in a windows service and succeeded:

    (Set Service Account to LocalSystem instead of User to test)

    Code Snippet

    public class QuotaInformation

            {

                public String DiskSpaceUsed;

                public String Limit;

                public String QuotaVolume;

                public String User;

            }

            string str = string.Empty;

            void GetQuotaInformation()

            {

                ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"\root\cimv2",

                "SELECT DiskSpaceUsed, Limit, QuotaVolume, User FROM Win32_DiskQuota");

     

                foreach (ManagementObject queryObj in searcher.Get())

                {

                    str += "Disk Space Used: " + queryObj["DiskSpaceUsed"].ToString() + Environment.NewLine;

                    str += "Limit: " + queryObj["Limit"].ToString() + Environment.NewLine;

                    str += "QuotaVolume:" + queryObj["QuotaVolume"].ToString() + Environment.NewLine;

                    str += "User: " + queryObj["User"].ToString() + Environment.NewLine;

                    QuotaInformation quotaInfo = new QuotaInformation();

                    quotaInfo.DiskSpaceUsed = queryObj["DiskSpaceUsed"].ToString();

                    quotaInfo.Limit = queryObj["Limit"].ToString();

                    quotaInfo.QuotaVolume = queryObj["QuotaVolume"].ToString();

                    quotaInfo.User = queryObj["User"].ToString();

                }

            }

            protected override void OnStart(string[] args)

            {

                // TODO: Add code here to start your service.

                GetQuotaInformation();

                FileStream fs = new FileStream(@"D:\testService.txt", FileMode.OpenOrCreate, FileAccess.Write);

                StreamWriter m_streamWriter = new StreamWriter(fs);

                m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);

                m_streamWriter.WriteLine("testService:         Service Started" + DateTime.Now.ToString() + "\n");

                m_streamWriter.Write(str);

                m_streamWriter.Flush();

                m_streamWriter.Close();

                fs.Close();

            }

     

    Then check the testService.txt file under D: directory.

    Please give a try.

    Thanks

    • ReplyReply
    • QuoteQuote
     

All Replies

  • Thursday, June 28, 2007 6:12 AMFigo FeiMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    Hi,

    Please make sure the service is run under Local System which is privileged.

    Thanks

     

    • ReplyReply
    • QuoteQuote
     
Need Help with Forums? (FAQ)
 
© 2009 Microsoft Corporation. All rights reserved.
Terms of Use
|
Trademarks
|
Privacy Statement