Showing posts with label Build Action. Show all posts
Showing posts with label Build Action. Show all posts

Thursday, January 24, 2013

Check if Internet connection is available with C#

There can be situations when you need to check, whether your application has Internet access. 
Maybe you have to download/upload some file or database, or just run some application check. 
In my case, my application mainly developed based on Service Oriented Architecture. So application must have an Internet connection to work.
There are plenty different ways to archive this. I will describe the one which is very easier first.
This is the method.


System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()


If you want to check whether the internet connection is available on application life time, you can have a timer and check whether the connection is available. If not you can set warning for the user.
Here how I done that in WPF.


using System.Threading;
using System;
namespace Example
{
 public partial class MainWindow : Window
 {
        private Timer timer;
        public MainWindow()
        {
            InitializeComponent();
            if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
            { 
               MessageBox.Show("Internet connection not available, \n Please check your connection");
               return;
            }
            timer = new Timer(UpdateNetworkStatus, null, new TimeSpan(0, 0, 0, 0, 500), new TimeSpan(0, 0, 0, 0, 500));
            lblNetworkStatus.Visibility = Visibility.Collapsed;
        }
        private void UpdateNetworkStatus(object o)
        {
            if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
                lblNetworkStatus.Visibility = Visibility.Visible;
            else
                lblNetworkStatus.Visibility = Visibility.Collapsed;
        }
 }
}

Will clean this a bit and describe the other ways once I got free time.
I just write this blog post because I thought that it will helpful for most of the developers.
Cheers!!!

Friday, January 4, 2013

How to ensure external files correctly get copied when publishing

Before I go into the topic I have to say, this is my first blog post. So It might have some issues. Please apologize me on that. :) The main reason I have motivated to write this blog is to give my experience on software development to out side world. Then they can over come the issues I faced really easily.
Lets start.
Yesterday I was working on some project which associated with the SQLight. Actually project was working really fine on my developer machine and when it gets published it was stopped working on client machine as saying,
So I searched many blog posts and internet to solve that issue, but came up with nothing as I cant find exact way to search on that issue. :)
I did several experiments to over come that issue. Finally I came with a solution by changing the external file properties. Luckily it was solved.
Following is the way to follow that.
  1.  Go to solution explorer
  2.  Right click on the file you want to send with the release
  3.  Click on properties
  4.  Set Build Action into Content
  5.  Set Copy to output directory into Copy if newer or Copy always ( I will discuss about the difference of those later)
  6.  Publish :)

Yes. That't it. Hope It will help you.
Cheers!...