9 Replies - 1653 Views - Last Post: 05 October 2012 - 08:14 AM Rate Topic: -----

#1 Lancelotlink  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-October 12

C# webclient Issue

Posted 04 October 2012 - 12:44 PM

Hello everyone, I'm new to C# so please bare with me. I've not done any programming before and just decided to take this little task on myself since a friend of mine said it was a relatively easy project to do in Visual Basic. I've learned a lot over the past week thanks to this and the "Search" function and Google.

So, my work uses a VPN for me to be able to remotely log in whenever I need to. However the configuration changes frequently. So, instead of downloading a file every day and overwriting the existing configuration file manually I wanted it to be automated.

So far, here's what I have:
using System;
using System.Linq;
using System.IO;
using System.Net;
using System.Windows.Forms;
using System.Diagnostics;

namespace Config_Updater
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Uri uri = new Uri("https://myupdatedconfigfile.com");
            string filename = @"C:\Program Files\OpenVPN\config";
            {
            begin:
                Process[] pname = Process.GetProcessesByName("OpenVPN"); 

                if (pname.Length == 1)
                {
                    //string processName = "OpenVPN";

                    foreach (Process proc in Process.GetProcessesByName("OpenVPN"))
                        proc.Kill();
                    goto begin;
                }
                try
                {
                    if (File.Exists(filename))
                    {
                        File.Delete(filename);
                    }
                    else
                    {
                        WebClient wc = new WebClient();
                        wc.DownloadFile(uri, filename);
                     }
                 finally
                {
                    Application.Exit();
                        } 
              }
            }
    }
}


So, I want the program to accomplish several things.

  • If the OpenVPN process is already running I want it to close it.
  • I want it to identify and delete the old config file to ensure it is completely removed.
  • Download the most current Config file for OpenVPN and then end application.




However, when I debug this program I get a WebClient was unable to initialize type error. Am I missing a step completely? Did I not include a specific "using System"?

Any and all help you can provide me with would be very much appreciated. :bigsmile:

Is This A Good Question/Topic? 0
  • +

Replies To: C# webclient Issue

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3468
  • View blog
  • Posts: 10,688
  • Joined: 05-May 12

Re: C# webclient Issue

Posted 04 October 2012 - 12:57 PM

Get rid of that goto. You can accomplish the same thing using a while or do-while loop.

Then is no need to call Application.Exit(). Your program will exit by itself after the finally clause.

What is the exact error you are getting when you instantiate the WebClient object?

And I'd rather not bare with you. My preferences are towards the opposite sex and I'd rather bare with them. Unfortunately, my significant other has claimed exclusive rights so I'm only allow to bare with her. On the other hand, I am willing to bear with you.

This post has been edited by Skydiver: 04 October 2012 - 12:59 PM

Was This Post Helpful? 0
  • +
  • -

#3 Lancelotlink  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-October 12

Re: C# webclient Issue

Posted 04 October 2012 - 02:08 PM

View PostSkydiver, on 04 October 2012 - 12:57 PM, said:

Get rid of that goto. You can accomplish the same thing using a while or do-while loop.

Then is no need to call Application.Exit(). Your program will exit by itself after the finally clause.

What is the exact error you are getting when you instantiate the WebClient object?

And I'd rather not bare with you. My preferences are towards the opposite sex and I'd rather bare with them. Unfortunately, my significant other has claimed exclusive rights so I'm only allow to bare with her. On the other hand, I am willing to bear with you.



Yes, please "bear" with me as I'm not interested in you "baring" anything for me whatsoever. Keep that :bananaman: to your wife please.

Are you suggesting something along the lines of this?
using System;
	using System.Linq;
	using System.IO;
	using System.Net;
	using System.Windows.Forms;
	using System.Diagnostics;
	 
	namespace Config_Updater
	{
	    class Program
	    {
	        [STAThread]
	        static void Main(string[] args)
	        {
	            Uri uri = new Uri("https://myupdatedconfigfile.com");

	            string filename = @"C:\Program Files\OpenVPN\config";
	            {
	            begin:
	                Process[] pname = Process.GetProcessesByName("OpenVPN"); 
	 
	                if (pname.Length == 1)
	                {
	                    //string processName = "OpenVPN";
	 
	                    foreach (Process proc in Process.GetProcessesByName("OpenVPN"))
	                        proc.Kill();
	                    While(process.exist = true)
                            {
                            Thread.Sleep(10);
                            }
	                    try
	                    {
	                    if (File.Exists(filename))
	                    {
	                        File.Delete(filename);
	                    }
	                    else
	                    {
	                        WebClient wc = new WebClient();
	                        wc.DownloadFile(uri, filename);
	                     }
	                 finally
	              }
	            }
	          }

As for the exact error msg I'll post it up shortly once I get back in front of my computer with Visual Studio on it. I'm working through notepad at the moment.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3468
  • View blog
  • Posts: 10,688
  • Joined: 05-May 12

Re: C# webclient Issue

Posted 04 October 2012 - 04:37 PM

Something like this psuedo code:
get array of processes
while (array length > 0)
{
    foreach(process in array)
        process.Kill()

    get array of processes
}

try
{
    download new config file to temporary file
    rename old config file to a scatch name
    rename temporary file to new config file
    delete the scratch file
}
catch()
{
    report error about downloads or renames
}



The reason for the renames is for the off chance that another instance of OpenCV starts up and is holdig on to the file, Windows will usually let you rename a file that is being held open (this is how Windows can sometimes update DLLs even if a process is holding on to it and let you defer rebooting until later).

This post has been edited by Skydiver: 04 October 2012 - 04:40 PM

Was This Post Helpful? 0
  • +
  • -

#5 Lancelotlink  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-October 12

Re: C# webclient Issue

Posted 04 October 2012 - 07:31 PM

Thanks, I'll try my luck at working that into my code.

Regarding the exception I get when debugging I'm getting:
"A first chance exception of type 'System.Net.WebException' occurred in System.dll"

It is specifically pointing out the "wc.DownloadFile(uri, filename);" code as the source of the exception.

Thoughts?

I get the

View PostSkydiver, on 04 October 2012 - 04:37 PM, said:

Something like this psuedo code:
get array of processes
while (array length > 0)
{
    foreach(process in array)
        process.Kill()

    get array of processes
}

try
{
    download new config file to temporary file
    rename old config file to a scatch name
    rename temporary file to new config file
    delete the scratch file
}
catch()
{
    report error about downloads or renames
}



The reason for the renames is for the off chance that another instance of OpenCV starts up and is holdig on to the file, Windows will usually let you rename a file that is being held open (this is how Windows can sometimes update DLLs even if a process is holding on to it and let you defer rebooting until later).

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3468
  • View blog
  • Posts: 10,688
  • Joined: 05-May 12

Re: C# webclient Issue

Posted 04 October 2012 - 09:53 PM

If you click on the Details button of the exception popup that Visual Studio brings up, what does it say?
Was This Post Helpful? 0
  • +
  • -

#7 Lancelotlink  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-October 12

Re: C# webclient Issue

Posted 05 October 2012 - 05:41 AM

It says "Access to the path 'C:\\Program Files\\OpenVPN\\config\\iGl1eIgt9y.ovpn' is denied."}

I double checked and OpenVPN is not running in the task manager and the file is not set as read only.
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,429
  • Joined: 23-August 08

Re: C# webclient Issue

Posted 05 October 2012 - 06:01 AM

Writing to Program Files requires elevated permissions. Are you running as Administrator? Does the file exist already?
Was This Post Helpful? 1
  • +
  • -

#9 Lancelotlink  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-October 12

Re: C# webclient Issue

Posted 05 October 2012 - 06:48 AM

View PostJackOfAllTrades, on 05 October 2012 - 06:01 AM, said:

Writing to Program Files requires elevated permissions. Are you running as Administrator? Does the file exist already?


I'm admin on the computer but I haven't added a manifest requiring elevated permissions. I can add that. Yes, the file does already exist.
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4433
  • View blog
  • Posts: 7,702
  • Joined: 08-June 10

Re: C# webclient Issue

Posted 05 October 2012 - 08:14 AM

If this is Vista or higher (or server 2k8) you'll need that manifest, or you'll need to right-click and run the executable as admin, even if you're an administrator on the computer, because of UAC. Program Files is one of the directories requiring elevated perms.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1