InternetAccessIsAvailable() public static method

public static InternetAccessIsAvailable ( ) : bool
return bool
Example #1
0
 void appTimer_Tick(object sender, EventArgs e)
 {
     //Reload the application content, if there is still an internet connection
     if (NetworkHelper.InternetAccessIsAvailable())
     {
         UpdateContent();
     }
     //Update the timer to fire 24 hours from now
     _appTimer.Interval = 86400000;             //milliseconds in 24 hours
 }
Example #2
0
 private void TestInternetConnection()
 {
     _internetAvailable = NetworkHelper.InternetAccessIsAvailable();
 }
Example #3
0
        static void Main()
        {
            //Checking to see if logging is enabled.
            GlobalVariables.LoggingEnabled = Settings.Default.LoggingEnabled;
            GlobalVariables.GetApplicationVersion();

            if (GlobalVariables.LoggingEnabled)
            {
                ExceptionManager.WriteInformation("Application starting.");
            }

            //Checking to see if the application is running
            if (Mutex.WaitOne(TimeSpan.Zero, true))
            {
                if (GlobalVariables.LoggingEnabled)
                {
                    ExceptionManager.WriteInformation("Checking internet connection.");
                }
                if (!NetworkHelper.InternetAccessIsAvailable())
                {
                    MessageBox.Show(Resources.InternetRequiredMessage, Resources.InternetRequiredTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (GlobalVariables.LoggingEnabled)
                    {
                        ExceptionManager.WriteInformation("No internet connection. Application exiting.");
                    }
                    Environment.Exit(Environment.ExitCode);
                }
                else
                {
                    //Application is not currently running and there is an available internet connection

                    //Check for updates
                    if (GlobalVariables.LoggingEnabled)
                    {
                        ExceptionManager.WriteInformation("Checking for application update.");
                    }
                    if (ApplicationUpdater.UpdateIsAvailable())
                    {
                        //Kick off the updater
                        var dlgResult = MessageBox.Show(Resources.UpdateQuestion, Resources.AnUpdateIsAvailable,
                                                        MessageBoxButtons.YesNo);
                        if (dlgResult == DialogResult.Yes)
                        {
                            //Need the 'runas' verb to allow admin privileges for update
                            var startInfo = new ProcessStartInfo("Updater.exe")
                            {
                                Verb = "runas"
                            };
                            Process.Start(startInfo);
                        }
                        else
                        {
                            Application.Run(new PicofDay());
                        }
                    }
                    else
                    {
                        Application.Run(new PicofDay());
                    }
                }
            }
            else
            {
                //If application is already running and there is no internet connection available, close the application
                if (GlobalVariables.LoggingEnabled)
                {
                    ExceptionManager.WriteInformation("Application is already running. But there is no internet connection. Exiting the application.");
                }
                if (!NetworkHelper.InternetAccessIsAvailable())
                {
                    MessageBox.Show(Resources.InternetRequiredMessage, Resources.InternetRequiredTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Application.ExitThread();
                }
            }
        }
Example #4
0
        /// <summary>
        /// Overloaded GetImage that allows the retrieval of previous images based on their position in the XML document
        /// provided from the nasa.gov website.
        /// </summary>
        /// <param name="selectedOffset">Position of the image within the node list of images, 1 being the very first (newest)</param>
        /// <param name="selectedPage">Page number to retrieve the page that contains the selected image.</param>
        public BackgroundImage GetImage(int?selectedOffset, int?selectedPage)
        {
            try
            {
                if (!NetworkHelper.InternetAccessIsAvailable())
                {
                    throw new Exception("Attempted to retrieve image, but internet connection was not available.");
                }

                if (GlobalVariables.LoggingEnabled)
                {
                    ExceptionManager.WriteInformation("Retrieving system screen resolution");
                }
                GetCurrentScreenResolution();
                if (GlobalVariables.LoggingEnabled)
                {
                    ExceptionManager.WriteInformation(string.Format("System screen resolution is: {0}", _currentScreenResolution));
                }

                if (selectedOffset == null)
                {
                    selectedOffset = DefaultimageOffset;
                }

                //Get the JSON string data
                if (GlobalVariables.LoggingEnabled)
                {
                    ExceptionManager.WriteInformation("Preparing to download image information");
                }
                var nasaImages = JsonHelper.DownloadSerializedJsonData(string.Format(NasaLatestImagesUrl, selectedPage));
                if (nasaImages == null || nasaImages.UberNodes.Length == 0)
                {
                    throw new Exception("Unable to retrieve image data from JSON request");
                }

                //Get the image node
                var imageId = nasaImages.UberNodes[(int)selectedOffset].UberNodeId;

                var currentImageFullUrl = string.Format("{0}{1}.json", NasaApiUrl, imageId);

                var currentImage = JsonHelper.DownloadImageData(currentImageFullUrl);
                if (currentImage == null)
                {
                    throw new Exception(string.Format("Unable to retrieve selected image: {0}", currentImageFullUrl));
                }

                if (GlobalVariables.LoggingEnabled)
                {
                    ExceptionManager.WriteInformation("Selecting image based on current screen resolution");
                }
                switch (_currentScreenResolution)
                {
                case "100x75":
                case "226x170":
                    currentImageFullUrl = string.Format("{0}{1}", NasaImageBaseUrl, currentImage.ImageData[0].LrThumbnail);
                    break;

                case "346x260":
                case "360x225":
                case "466x248":
                case "430x323":
                    currentImageFullUrl = string.Format("{0}{1}", NasaImageBaseUrl, currentImage.ImageData[0].Crop1X1);
                    break;

                case "800x600":
                    currentImageFullUrl = string.Format("{0}{1}", NasaImageBaseUrl, currentImage.ImageData[0].Crop2X1);
                    break;

                default:
                    currentImageFullUrl = string.Format("{0}{1}", NasaImageBaseUrl, currentImage.ImageData[0].FullWidthFeature);
                    break;
                }

                var localImageFolderPath = string.Format("{0}\\NASA\\PicOfTheDay", Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));

                if (!Directory.Exists(localImageFolderPath))
                {
                    Directory.CreateDirectory(localImageFolderPath);
                }

                var imageName = currentImage.ImageData[0].FileName;

                //Setting the full local image path to save the file
                var fullImagePath = string.Format("{0}\\{1}", localImageFolderPath, imageName);

                //Download the current image
                if (GlobalVariables.LoggingEnabled)
                {
                    ExceptionManager.WriteInformation(string.Format("Preparing to download image: {0}", currentImageFullUrl));
                }
                if (!DownloadHelper.DownloadImage(fullImagePath, currentImageFullUrl))
                {
                    throw new Exception("Error downloading current image.");
                }

                //Create BackgroundImage object
                var backgroundImage = new BackgroundImage
                {
                    ImageUrl         = currentImageFullUrl,
                    ImageDate        = Convert.ToDateTime(currentImage.UberData.PromoDateTime),
                    ImageDescription = StripHtml(currentImage.UberData.Body),
                    DownloadedPath   = fullImagePath,
                    ImageTitle       =
                        !string.IsNullOrEmpty(currentImage.ImageData[0].Title)
                     ? currentImage.ImageData[0].Title
                     : currentImage.UberData.Title
                };

                return(backgroundImage);
            }
            catch (Exception ex)
            {
                ExceptionManager.WriteException(ex);
                return(null);
            }
        }