Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows.Forms;
- using System.Xml;
- using System.Diagnostics;
- using System.IO;
- namespace RapidConnect
- {
- static class Program
- {
- [STAThread]
- public static int OpenConnection(string connectionName, string UserName, string Password, int Timeout)
- {
- int ExitCode;
- ProcessStartInfo ProcessInfo;
- Process Process;
- ProcessInfo = new ProcessStartInfo("cmd.exe", "/C rasdial \"" + connectionName + "\" \"" + UserName + "\" \"" + Password + "\"");
- ProcessInfo.CreateNoWindow = true;
- ProcessInfo.UseShellExecute = false;
- Process = Process.Start(ProcessInfo);
- Process.WaitForExit(Timeout);
- ExitCode = Process.ExitCode;
- Process.Close();
- return ExitCode;
- }
- public static int CloseConnection(string connectionName, int Timeout)
- {
- int ExitCode;
- ProcessStartInfo ProcessInfo;
- Process Process;
- ProcessInfo = new ProcessStartInfo("cmd.exe", "/C rasdial \"" + connectionName + "\" /disconnect");
- ProcessInfo.CreateNoWindow = true;
- ProcessInfo.UseShellExecute = false;
- Process = Process.Start(ProcessInfo);
- Process.WaitForExit(Timeout);
- ExitCode = Process.ExitCode;
- Process.Close();
- return ExitCode;
- }
- static int Main()
- {
- string rsUsername, rsPassword, cUsername, cPassword, cName;
- int rsAccountType, cTimeOut;
- int minspeed;
- XmlDocument xDoc = new XmlDocument();
- xDoc.Load("data.xml");
- XmlNode root = xDoc.FirstChild.NextSibling;
- XmlNode xn;
- xn = root.FirstChild;
- try
- {
- minspeed = int.Parse(root.Attributes["minimum_speed"].Value);
- while (xn != null && xn.Name != "rapidshare") xn = xn.NextSibling;
- if (xn.Name == "rapidshare")
- {
- rsUsername = xn.Attributes["username"].Value;
- rsPassword = xn.Attributes["password"].Value;
- rsAccountType = int.Parse(xn.Attributes["account_type"].Value);
- }
- else
- {
- Console.Write("Xml file structure error.\nPress Enter to exit.");
- Console.ReadLine();
- return 1;
- }
- xn = root.FirstChild;
- while (xn != null && xn.Name != "internet_connection") xn = xn.NextSibling;
- if (xn.Name == "internet_connection")
- {
- cUsername = xn.Attributes["username"].Value;
- cPassword = xn.Attributes["password"].Value;
- cName = xn.Attributes["connection_name"].Value;
- cTimeOut = int.Parse(xn.Attributes["timeout"].Value);
- }
- else
- {
- Console.Write("Xml file structure error.\nPress Enter to exit.");
- Console.ReadLine();
- return 1;
- }
- }
- catch (Exception e)
- {
- Console.Write("XML file error: "+ e.Message + "\nPress Enter to exit");
- Console.ReadLine();
- return 2;
- }
- bool exit = false;
- double speed = 0;
- DateTime start, stop;
- int ticks;
- FileInfo f = new FileInfo("uploadfile");
- double filesize = f.Length/1024;
- while (!exit && speed < minspeed)
- {
- Console.Write("Starting connection... ");
- int rv;
- rv = OpenConnection(cName, cUsername, cPassword, cTimeOut);
- //rv = 0;
- if (rv == 0)
- Console.Write("Done.\n");
- else
- {
- Console.Write("\nConnection failed.\nRASDIAL terminated with exit code: " + rv.ToString() + "\nPress Enter to exit.");
- Console.ReadLine();
- return 3;
- }
- start = DateTime.Now;
- Console.Write("Starting upload...\n");
- QRapidshare rs = new QRapidshare();
- rs.QUploadToRapidshare("uploadfile", rsUsername, rsPassword, 1);
- Console.Write("Upload (maybe) finished.\n");
- stop = DateTime.Now;
- ticks = (int)((TimeSpan)(stop - start)).TotalMilliseconds;
- speed = filesize / ticks * 1000;
- Console.Write("Upload speed: " + String.Format("{0:0.00}", speed)+"kB/s\n");
- if (Console.KeyAvailable)
- {
- ConsoleKeyInfo cki = Console.ReadKey();
- if (cki.Key == ConsoleKey.Q)
- exit = true;
- }
- if (!exit && speed < minspeed)
- {
- Console.Write("Terminating connection... ");
- rv = CloseConnection(cName, cTimeOut);
- //rv = 0;
- if (rv == 0) Console.Write("Done\n");
- else
- {
- Console.Write("\nDeconnection failed.\nRASDIAL terminated with exit code: " + rv.ToString() + "\nPress Enter to exit.");
- Console.ReadLine();
- return 4;
- }
- }
- }
- Console.Write("\nPress ENTER to exit.");
- Console.ReadLine();
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement