-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
117 lines (107 loc) · 3.99 KB
/
MainWindow.xaml.cs
File metadata and controls
117 lines (107 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Text.Json;
using Windows.Storage.Pickers;
using WinRT.Interop;
namespace VncRdpLauncher
{
public sealed partial class MainWindow : Window
{
private ObservableCollection<ConnectionItem> connections = new();
private string savePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "VncRdpLauncher", "connections.json");
public MainWindow()
{
this.InitializeComponent();
FileList.ItemsSource = connections;
LoadConnections();
}
private async void AddFile_Click(object sender, RoutedEventArgs e)
{
var picker = new FileOpenPicker();
picker.FileTypeFilter.Add(".vnc");
picker.FileTypeFilter.Add(".rdp");
// Required for WinUI 3
var hwnd = WindowNative.GetWindowHandle(this);
InitializeWithWindow.Initialize(picker, hwnd);
var file = await picker.PickSingleFileAsync();
if (file != null)
{
connections.Add(new ConnectionItem { Name = file.Name, Path = file.Path });
SaveConnections();
}
}
private void RemoveFile_Click(object sender, RoutedEventArgs e)
{
if (FileList.SelectedItem is ConnectionItem item)
{
connections.Remove(item);
SaveConnections();
}
}
private void FileList_DoubleTapped(object sender, Microsoft.UI.Xaml.Input.DoubleTappedRoutedEventArgs e)
{
if (FileList.SelectedItem is ConnectionItem item)
{
try
{
if (item.Path.EndsWith(".vnc", StringComparison.OrdinalIgnoreCase))
{
Process.Start(new ProcessStartInfo
{
FileName = @"C:\Program Files\UltraVNC\vncviewer.exe",
Arguments = $"\"{item.Path}\"",
UseShellExecute = true
});
}
else if (item.Path.EndsWith(".rdp", StringComparison.OrdinalIgnoreCase))
{
Process.Start(new ProcessStartInfo
{
FileName = "mstsc.exe",
Arguments = $"\"{item.Path}\"",
UseShellExecute = true
});
}
}
catch (Exception ex)
{
var dialog = new ContentDialog
{
Title = "Error",
Content = ex.Message,
CloseButtonText = "OK",
XamlRoot = this.Content.XamlRoot
};
_ = dialog.ShowAsync();
}
}
}
private void SaveConnections()
{
Directory.CreateDirectory(Path.GetDirectoryName(savePath)!);
File.WriteAllText(savePath, JsonSerializer.Serialize(connections));
}
private void LoadConnections()
{
if (File.Exists(savePath))
{
var data = File.ReadAllText(savePath);
var loaded = JsonSerializer.Deserialize<ObservableCollection<ConnectionItem>>(data);
if (loaded != null)
{
foreach (var item in loaded)
connections.Add(item);
}
}
}
}
public class ConnectionItem
{
public required string Name { get; set; }
public required string Path { get; set; }
}
}