Advertisement
Guest User

Untitled

a guest
May 10th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5. using System.Xml;
  6. using System.Diagnostics;
  7. using System.IO;
  8.  
  9. namespace RapidConnect
  10. {
  11.     static class Program
  12.     {
  13.         [STAThread]
  14.         public static int OpenConnection(string connectionName, string UserName, string Password, int Timeout)
  15.         {
  16.             int ExitCode;
  17.             ProcessStartInfo ProcessInfo;
  18.             Process Process;
  19.  
  20.             ProcessInfo = new ProcessStartInfo("cmd.exe", "/C rasdial \"" + connectionName + "\" \"" + UserName + "\" \"" + Password + "\"");
  21.             ProcessInfo.CreateNoWindow = true;
  22.             ProcessInfo.UseShellExecute = false;
  23.             Process = Process.Start(ProcessInfo);
  24.             Process.WaitForExit(Timeout);
  25.             ExitCode = Process.ExitCode;
  26.             Process.Close();
  27.  
  28.             return ExitCode;
  29.         }
  30.  
  31.         public static int CloseConnection(string connectionName, int Timeout)
  32.         {
  33.             int ExitCode;
  34.             ProcessStartInfo ProcessInfo;
  35.             Process Process;
  36.  
  37.             ProcessInfo = new ProcessStartInfo("cmd.exe", "/C rasdial \"" + connectionName + "\" /disconnect");
  38.             ProcessInfo.CreateNoWindow = true;
  39.             ProcessInfo.UseShellExecute = false;
  40.             Process = Process.Start(ProcessInfo);
  41.             Process.WaitForExit(Timeout);
  42.             ExitCode = Process.ExitCode;
  43.             Process.Close();
  44.  
  45.             return ExitCode;
  46.         }
  47.  
  48.         static int Main()
  49.         {
  50.             string rsUsername, rsPassword, cUsername, cPassword, cName;
  51.             int rsAccountType, cTimeOut;
  52.             int minspeed;
  53.  
  54.             XmlDocument xDoc = new XmlDocument();
  55.             xDoc.Load("data.xml");
  56.  
  57.             XmlNode root = xDoc.FirstChild.NextSibling;
  58.             XmlNode xn;
  59.  
  60.             xn = root.FirstChild;
  61.  
  62.             try
  63.             {
  64.                 minspeed = int.Parse(root.Attributes["minimum_speed"].Value);
  65.                 while (xn != null && xn.Name != "rapidshare") xn = xn.NextSibling;
  66.                 if (xn.Name == "rapidshare")
  67.                 {
  68.                     rsUsername = xn.Attributes["username"].Value;
  69.                     rsPassword = xn.Attributes["password"].Value;
  70.                     rsAccountType = int.Parse(xn.Attributes["account_type"].Value);
  71.                 }
  72.                 else
  73.                 {
  74.                     Console.Write("Xml file structure error.\nPress Enter to exit.");
  75.                     Console.ReadLine();
  76.                     return 1;
  77.                 }
  78.  
  79.                 xn = root.FirstChild;
  80.                 while (xn != null && xn.Name != "internet_connection") xn = xn.NextSibling;
  81.                 if (xn.Name == "internet_connection")
  82.                 {
  83.                     cUsername = xn.Attributes["username"].Value;
  84.                     cPassword = xn.Attributes["password"].Value;
  85.                     cName = xn.Attributes["connection_name"].Value;
  86.                     cTimeOut = int.Parse(xn.Attributes["timeout"].Value);
  87.                 }
  88.                 else
  89.                 {
  90.                     Console.Write("Xml file structure error.\nPress Enter to exit.");
  91.                     Console.ReadLine();
  92.                     return 1;
  93.                 }
  94.             }
  95.             catch (Exception e)
  96.             {
  97.                 Console.Write("XML file error: "+ e.Message + "\nPress Enter to exit");
  98.                 Console.ReadLine();
  99.                 return 2;
  100.             }
  101.  
  102.             bool exit = false;
  103.             double speed = 0;
  104.  
  105.             DateTime start, stop;
  106.             int ticks;
  107.             FileInfo f = new FileInfo("uploadfile");
  108.             double filesize = f.Length/1024;
  109.  
  110.             while (!exit && speed < minspeed)
  111.             {
  112.                 Console.Write("Starting connection... ");
  113.                 int rv;
  114.                 rv = OpenConnection(cName, cUsername, cPassword, cTimeOut);
  115.                 //rv = 0;
  116.                 if (rv == 0)
  117.                     Console.Write("Done.\n");
  118.                 else
  119.                 {
  120.                     Console.Write("\nConnection failed.\nRASDIAL terminated with exit code: " + rv.ToString() + "\nPress Enter to exit.");
  121.                     Console.ReadLine();
  122.                     return 3;
  123.                 }
  124.  
  125.                 start = DateTime.Now;
  126.  
  127.                 Console.Write("Starting upload...\n");
  128.                 QRapidshare rs = new QRapidshare();
  129.                 rs.QUploadToRapidshare("uploadfile", rsUsername, rsPassword, 1);
  130.                 Console.Write("Upload (maybe) finished.\n");
  131.  
  132.                 stop = DateTime.Now;
  133.                 ticks = (int)((TimeSpan)(stop - start)).TotalMilliseconds;
  134.                 speed = filesize / ticks * 1000;
  135.                 Console.Write("Upload speed: " + String.Format("{0:0.00}", speed)+"kB/s\n");
  136.  
  137.                 if (Console.KeyAvailable)
  138.                 {
  139.                     ConsoleKeyInfo cki = Console.ReadKey();
  140.                     if (cki.Key == ConsoleKey.Q)
  141.                         exit = true;
  142.                 }
  143.  
  144.                 if (!exit && speed < minspeed)
  145.                 {
  146.                     Console.Write("Terminating connection... ");
  147.                     rv = CloseConnection(cName, cTimeOut);
  148.                     //rv = 0;
  149.                     if (rv == 0) Console.Write("Done\n");
  150.                     else
  151.                     {
  152.                         Console.Write("\nDeconnection failed.\nRASDIAL terminated with exit code: " + rv.ToString() + "\nPress Enter to exit.");
  153.                         Console.ReadLine();
  154.                         return 4;
  155.                     }
  156.                 }
  157.             }
  158.            
  159.             Console.Write("\nPress ENTER to exit.");
  160.             Console.ReadLine();
  161.             return 0;
  162.         }
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement