MossyUpdater/MainWindow.xaml.cs

72 lines
2.3 KiB
C#

using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows;
using Ookii.Dialogs.Wpf; // ← add this
namespace MossyUpdater
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Browse_Click(object sender, RoutedEventArgs e)
{
var dlg = new VistaFolderBrowserDialog
{
Description = "Select a destination folder",
UseDescriptionForTitle = true
};
// ShowDialog returns true if the user clicked OK
if (dlg.ShowDialog() == true)
FolderTextBox.Text = dlg.SelectedPath;
}
private async void Download_Click(object sender, RoutedEventArgs e)
{
string url = UrlTextBox.Text.Trim();
string folder = FolderTextBox.Text.Trim();
string filename = FilenameTextBox.Text.Trim();
if (string.IsNullOrEmpty(url)
|| string.IsNullOrEmpty(folder)
|| string.IsNullOrEmpty(filename))
{
StatusTextBlock.Text = "Please fill in all fields.";
StatusTextBlock.Foreground = System.Windows.Media.Brushes.Red;
return;
}
string fullPath = Path.Combine(folder, filename);
try
{
using var client = new HttpClient();
var data = await client.GetByteArrayAsync(url);
await File.WriteAllBytesAsync(fullPath, data);
UnblockFile(fullPath);
StatusTextBlock.Text = $"Downloaded and unblocked:\n{fullPath}";
StatusTextBlock.Foreground = System.Windows.Media.Brushes.Green;
}
catch (Exception ex)
{
StatusTextBlock.Text = $"Error: {ex.Message}";
StatusTextBlock.Foreground = System.Windows.Media.Brushes.Red;
}
}
private void UnblockFile(string path)
{
var zone = path + ":Zone.Identifier";
if (File.Exists(zone))
File.Delete(zone);
}
}
}