Le réseau pour les développeurs > Forums - Accueil > Visual C# General > Windows service and Win32_DiskQuota
Poser une questionPoser une question
 

TraitéeWindows service and Win32_DiskQuota

  • lundi 25 juin 2007 00:18hedwig Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

    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.

Réponses

  • jeudi 28 juin 2007 07:57Figo FeiMSFT, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée

    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

Toutes les réponses

  • jeudi 28 juin 2007 06:12Figo FeiMSFT, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

    Hi,

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

    Thanks

     

  • jeudi 28 juin 2007 07:57Figo FeiMSFT, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée

    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