Downloader Ver.I
<Window x:Class="Downloader_Ver.I.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Downloader_Ver.I" mc:Ignorable="d" Title="Download Ver.I" Height="350" Width="515"> <Grid> <!--1--> <TextBlock HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="URL" VerticalAlignment="Top"/> <TextBox Height="18" HorizontalAlignment="Left" Margin="70,10,0,0" Name="URL" VerticalAlignment="Top" Width="330" /> <Button Content="Copylist" Height="18" HorizontalAlignment="Left" Margin="405,10,0,0" x:Name="URLButton" VerticalAlignment="Top" Width="88" Click="URLPath" /> <!--2--> <TextBlock HorizontalAlignment="Left" Margin="10,30,0,0" TextWrapping="Wrap" Text="Save Path" VerticalAlignment="Top"/> <TextBox Height="18" HorizontalAlignment="Left" Margin="70,30,0,0" Name="SavePathTextBox" VerticalAlignment="Top" Width="330" /> <Button Content="Save" Height="18" HorizontalAlignment="Left" Margin="405,30,0,0" x:Name="SavePathButton" VerticalAlignment="Top" Width="88" Click="SavePath" /> <!--3--> <TextBlock HorizontalAlignment="Left" Margin="10,50,0,0" TextWrapping="Wrap" Text="Username" VerticalAlignment="Top"/> <TextBox Height="18" HorizontalAlignment="Left" Margin="70,50,0,0" Name="Username" VerticalAlignment="Top" Width="330" /> <Button Content="Copylist" Height="18" HorizontalAlignment="Left" Margin="405,50,0,0" x:Name="UsernameCopyButton" VerticalAlignment="Top" Width="88" Click="GetUsername" /> <!--4--> <TextBlock HorizontalAlignment="Left" Margin="10,70,0,0" TextWrapping="Wrap" Text="Password" VerticalAlignment="Top"/> <TextBox Height="18" HorizontalAlignment="Left" Margin="70,70,0,0" Name="Password" VerticalAlignment="Top" Width="330" /> <Button Content="Copylist" Height="18" HorizontalAlignment="Left" Margin="405,70,0,0" x:Name="PasswordCopyButton" VerticalAlignment="Top" Width="88" Click="GetPassword" /> <!--5--> <TextBlock HorizontalAlignment="Left" Margin="10,90,0,0" TextWrapping="Wrap" Text="Speed" VerticalAlignment="Top"/> <TextBox Height="18" HorizontalAlignment="Left" Margin="70,90,0,0" Name="Speed" VerticalAlignment="Top" Width="330" /> <Button Content="Download" Height="18" HorizontalAlignment="Left" Margin="405,90,0,0" x:Name="DownloadButton" VerticalAlignment="Top" Width="88" Click="Download" /> <!--6--> <TextBox HorizontalAlignment="Left" Height="196" Margin="10,113,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="483" Name="Result"/> </Grid> </Window>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using Microsoft.WindowsAPICodePack.Dialogs; using System.IO; using System.Diagnostics; using System.Windows.Threading; using System.Threading; namespace Downloader_Ver.I { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public Thread cmd; public MainWindow() { InitializeComponent(); Username.Text = ""; Password.Text = ""; URL.Text = ""; SavePathTextBox.Text = ""; Speed.Text = ""; } private void URLPath(object sender, RoutedEventArgs e) { } private void SavePath(object sender, RoutedEventArgs e) { var dialog = new CommonOpenFileDialog { IsFolderPicker = true }; if (dialog.ShowDialog() == CommonFileDialogResult.Ok) { SavePathTextBox.Text = dialog.FileName; } } private void GetUsername(object sender, RoutedEventArgs e) { } private void GetPassword(object sender, RoutedEventArgs e) { } private void Download(object sender, RoutedEventArgs e) { DownloadButton.Content = "Stop"; BuildWget(); BulidBat(); StartCmd(); } public void ProcessOuputHandler(object sendingProcess, DataReceivedEventArgs outLine) { if (!String.IsNullOrEmpty(outLine.Data)) { if (!Result.Dispatcher.CheckAccess()) { // Called from a none ui thread, so use dispatcher ShowLoggingDelegate showLoggingDelegate = new ShowLoggingDelegate(ShowLogging); Result.Dispatcher.Invoke(DispatcherPriority.Normal, showLoggingDelegate, outLine.Data); } else { // Called from UI trhead so just update the textbox ShowLogging(outLine.Data); Thread.Sleep(100); }; } } private delegate void ShowLoggingDelegate(string text); private delegate void ChangeButtonDelegate(); private static Action EmptyDelegate = delegate () { }; private static Action NullDelegate = delegate () { }; /// <summary> /// Show the logging on screen /// </summary> /// <param name="text"></param> private void ShowLogging(string text) { Result.AppendText(text+Environment.NewLine); Result.ScrollToEnd(); } private void ChangeStatic() { DownloadButton.Content = "Download"; File.Delete($"{SavePathTextBox.Text}\\DownloadVer.I.bat"); File.Delete($"{SavePathTextBox.Text}\\wget.exe"); } private void BuildWget() { File.WriteAllBytes($"{SavePathTextBox.Text}\\wget.exe", StringToHex(WgetHex())); } public static byte[] StringToHex(string hexStr) { int i; int len; byte[] hexByte; len = hexStr.Length / 2; len = (hexStr.Length % 2 == 1) ? len + 1 : len; hexByte = new byte[len]; for (i = 0; i < len; i++) { if ((i == len - 1) && (hexStr.Length % 2 == 1)) { hexByte[i] = Convert.ToByte(hexStr.Substring(i * 2, 1), 16); } else { hexByte[i] = Convert.ToByte(hexStr.Substring(i * 2, 2), 16); } } return hexByte; } private void BulidBat() { //File.Copy("wget.exe", $"{SavePathTextBox.Text}\\wget.exe", true); FileStream fileStream = new FileStream($"{SavePathTextBox.Text}\\DownloadVer.I.bat", FileMode.Create, FileAccess.Write); StreamWriter streamWriter = new StreamWriter(fileStream); streamWriter.Write($"wget.exe -c -e "); bool hasHttps = URL.Text[4] == 's'; bool hasUsername = Username.Text != ""; bool hasPassword = Password.Text != ""; bool hasSpeed = Speed.Text != ""; if (hasHttps) { if(hasUsername) { if(hasSpeed) { streamWriter.Write($"--limit-rate={Speed.Text} robots=off -x --user={Username.Text} --password={Password.Text} -m --no-check-certificate "); } else { streamWriter.Write($"robots=off -x --user={Username.Text} --password={Password.Text} -m --no-check-certificate "); } } else { if (hasSpeed) { streamWriter.Write($"--limit-rate={Speed.Text} robots=off -x -m --no-check-certificate "); } else { streamWriter.Write($"robots=off -x -m --no-check-certificate "); } } } else { if (hasUsername) { if (hasSpeed) { streamWriter.Write($"--limit-rate={Speed.Text} robots=off -x --user={Username.Text} --password={Password.Text} -m "); } else { streamWriter.Write($"robots=off -x --user={Username.Text} --password={Password.Text} -m "); } } else { if (hasSpeed) { streamWriter.Write($"--limit-rate={Speed.Text} robots=off -x -m "); } else { streamWriter.Write($"robots=off -x -m "); } } } streamWriter.Write($"\"{URL.Text}\""); streamWriter.Close(); } private void ProcExit(object sender, EventArgs e) { if (!Result.Dispatcher.CheckAccess()) { // Called from a none ui thread, so use dispatcher ChangeButtonDelegate changeButtonDelegate = new ChangeButtonDelegate(ChangeStatic); DownloadButton.Dispatcher.Invoke(DispatcherPriority.Normal, changeButtonDelegate); } else { // Called from UI trhead so just update the textbox ChangeStatic(); } } public void StartCmd() { ProcessStartInfo startInfo = new ProcessStartInfo { FileName = $"{SavePathTextBox.Text}\\DownloadVer.I.bat", Arguments = "2>&1", RedirectStandardError = true, RedirectStandardOutput = true, UseShellExecute = false, WorkingDirectory = SavePathTextBox.Text, CreateNoWindow = true }; new Thread(() => { using (Process process = new Process()) { process.StartInfo = startInfo; process.OutputDataReceived += new DataReceivedEventHandler(ProcessOuputHandler); process.EnableRaisingEvents = true; process.Exited += new EventHandler(ProcExit); process.Start(); process.BeginOutputReadLine(); while (!process.HasExited) { // Refresh WPF window here Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate); } } }).Start(); } } }