Solved

code for showing alert in windows forms using C#

Posted on 2009-03-31
11
2,777 Views
Last Modified: 2012-06-27

Can any one provide  code for creating alert for low diskspace using C# in my windows application form.
i have only one drive(say C drive). If available disk space reaches 80%, it should automatically show alert as low disk space.
can you help me i providing sample code.

i have taken button and writen code , but it checks only if press the button.
instead i need automatic alert which can pop up when available disk space reaches 80%.
private void btnSample_Click(object sender, EventArgs e)
        {
            
            ArrayList df = new ArrayList();
            DriveInfo[] driveListess = DriveInfo.GetDrives();
 
            
 
 
			
            // Loop through the list of drives
            
            
            foreach (DriveInfo driven in driveListess)
            {
                try
                {
                    if (driven.TotalFreeSpace == 0)
                    {
                        df.Add(driven.Name.ToString() + ": Disk is full . Please check ");
                    }
                    else
                    {
                        df.Add(driven.Name.ToString() + " Drive does not have space. Do you wish to delete some of the files? "); 
                     
                    }
                    
                }
 
                                                    
 
                 catch(Exception ex)
 
                {
                    FreeSpace = 0;
                    TotalSize = 0;
                    UsedSpace = 0;
                    Sweep = 0;
                    lblFileSystem.Text = "N/A";
                    lblFreeSpace.Text = "N/A";
                    lblUsedSpace.Text = "N/A";
                    lblTotalSize.Text = "N/A";
                }
                
            }
            string Alertmsg =  "      " ;
 
            foreach (string str in df)
            {
                Alertmsg += str + Environment.NewLine;
                
            }
             MessageBox.Show(Alertmsg.ToString(),"Alert",MessageBoxButtons.OK,MessageBoxIcon.Warning);
 
        }

Open in new window

0
Comment
Question by:coventri
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
11 Comments
 
LVL 11

Expert Comment

by:bmatumbura
ID: 24029118
Put a timer on the form and set it to execute every few seconds. Call your function from the Elapsed event of the time.
0
 
LVL 6

Expert Comment

by:Cebik
ID: 24029138

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Timer timer;
 
        public Form1()
        {
            InitializeComponent();
 
            timer = new Timer();
            timer.Interval = 5000; //every 5 sec for example
            timer.Tick += new EventHandler(timer_Tick);
            timer.Enabled = true;
        }
 
        void timer_Tick(object sender, EventArgs e)
        {
            //put yor methode here..
        }
    }
}

Open in new window

0
 
LVL 6

Expert Comment

by:Cebik
ID: 24029153
exactly :)
i was writing example :)
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 1

Author Comment

by:coventri
ID: 24029494
But i need code that gives alert automatically when disk space reaches 80%.

in the above code ,i have taken when the disk space is complete.

instead it shouls show alert when disk space reaches 80% of used space
0
 
LVL 6

Accepted Solution

by:
Cebik earned 250 total points
ID: 24029532
use this IF statement

if (driven.TotalFreeSpace < ((double)driven.TotalSize * 0.80d))

Open in new window

0
 
LVL 6

Expert Comment

by:Cebik
ID: 24029589

if (driven.TotalFreeSpace < ((double)driven.TotalSize * 0.80d))
{
    df.Add(driven.Name.ToString() + ": Disk low free space. " + HumanReadableSize(driven.TotalFreeSpace));
}
 
public string HumanReadableSize(double len)
{
    string[] sizes = { "B", "KB", "MB", "GB" };
    int order = 0;
    while (len >= 1024 && order + 1 < sizes.Length)
    {
        order++;
        len = len / 1024;
    }
    string result = String.Format("{0:0.##} {1}", len, sizes[order]);
    return result;
 
}

Open in new window

0
 
LVL 11

Expert Comment

by:bmatumbura
ID: 24029632
Cebi - Don't you think the expression should be:

if (driven.TotalFreeSpace < ((double)driven.TotalSize * 0.20d))

Open in new window

0
 
LVL 6

Expert Comment

by:Cebik
ID: 24029636
sorry :)
100 - 80 = 20%

if (driven.TotalFreeSpace < (driven.TotalSize * 0.20d))
            {
                df.Add(driven.Name.ToString() + ": Disk low free space. " + HumanReadableSize(driven.TotalFreeSpace));
            }

Open in new window

0
 
LVL 1

Author Comment

by:coventri
ID: 24030190

The problem here is

in any case it is running only else part

else
                        {

                            df.Add(driven.Name.ToString() + " Drive is Full . Please check");
                        }

even if i take 0.2 or 0.8 it is giving only else part as output

atached is my code in timer1

private void timer1_Tick(object sender, EventArgs e)
        {

            {

                ArrayList df = new ArrayList();
                DriveInfo[] driveListess = DriveInfo.GetDrives();





                // Loop through the list of drives


                foreach (DriveInfo driven in driveListess)
                {
                    try
                    {
                        if (driven.TotalFreeSpace < (driven.TotalSize * 0.20d))



                        {
                           
                            df.Add(driven.Name.ToString() + " Please check");
                        }
                        else
                        {

                            df.Add(driven.Name.ToString() + " Drive is Full . Please check");
                        }

                    }



                    catch (Exception ex)
                    {
                        FreeSpace = 0;
                        TotalSize = 0;
                        UsedSpace = 0;
                        Sweep = 0;
                        lblFileSystem.Text = "N/A";
                        lblFreeSpace.Text = "N/A";
                        lblUsedSpace.Text = "N/A";
                        lblTotalSize.Text = "N/A";
                    }

                }
                string Alertmsg = "      ";

                foreach (string str in df)
                {
                    Alertmsg += str + Environment.NewLine;

                }
                MessageBox.Show(Alertmsg.ToString(), "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }  

        }
0
 
LVL 1

Author Comment

by:coventri
ID: 24030208
it should only show alert when disk reaches 80% , rest of that it should not show any alert
0
 
LVL 11

Assisted Solution

by:bmatumbura
bmatumbura earned 250 total points
ID: 24030397
Additional checks will have to be done to achieve this. A possible solution would be to stop the timer just before the dialog is displayed. i.e. use

timer.Enabled = false;

just before the MessageBox.Show statement
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question