Şu kodlarımda ben bir checboxa birden fazla batch kodu eklettirmekiistiyorum using…
- User
Şu kodlarımda ben bir checboxa birden fazla batch kodu eklettirmekiistiyorum using System.IO; using System.Collections.Generic; using Microsoft.Win32; using Microsoft.Dism; using System.Text.Json; using ShadesToolkit.ViewModels.Pages; using Wpf.Ui.Controls; using System.Diagnostics; using System.ComponentModel; using System.Collections.ObjectModel; using System.Globalization; using System.Windows.Media; using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel; using System.Text; using System.Windows.Controls; using TreeViewItem = System.Windows.Controls.TreeViewItem;
namespace ShadesToolkit.Views.Pages { public partial class ExtraDebloat : INavigableView { private string MountFolder = Path.Combine(Path.GetTempPath(), "MountFolder");
public ExtraDebloatViewModel ViewModel { get; } private readonly Dictionary<CheckBox, string> checkBoxScripts = new Dictionary<CheckBox, string>(); public ExtraDebloat(ExtraDebloatViewModel viewModel) { ViewModel = viewModel; DataContext = this; InitializeComponent(); checkBoxScripts.Add(OneDrive, "MinSudo.exe --TrustedInstaller --Verbose --WorkDir=\"\" cmd /c \"del /S /Q \"%InstallDir%\\*OneDrive*\"\""); checkBoxScripts.Add(OneDrive, "MinSudo.exe --TrustedInstaller --Verbose --WorkDir=\"\" cmd /c \"del /S /Q \"%InstallDir%\\*onedrive-backup*\"\""); checkBoxScripts.Add(iki, "echo Batch Script 2"); checkBoxScripts.Add(uc, "echo Batch Script 3"); checkBoxScripts.Add(dort, "echo Batch Script 4"); } private void SaveButton_Click(object sender, RoutedEventArgs e) { StringBuilder scriptContent = new StringBuilder(); // Bat dosyasının en başına "echo off" ekler scriptContent.AppendLine("@echo off"); scriptContent.AppendLine("set InstallDir=C:\\Users\\%USERNAME%\\AppData\\Local\\Temp\\MountFolder"); // Her bir TreeViewItem için işleme foreach (TreeViewItem treeViewItem in treeView.Items) { ProcessTreeViewItem(treeViewItem, scriptContent); } if (scriptContent.Length > "@echo off".Length + Environment.NewLine.Length) { SaveScriptToFile(scriptContent.ToString()); } else { MessageBox.Show("Hiçbir komut seçilmedi."); } } private void ProcessTreeViewItem(ItemsControl itemsControl, StringBuilder scriptContent) { foreach (object obj in itemsControl.Items) { TreeViewItem subItem = itemsControl.ItemContainerGenerator.ContainerFromItem(obj) as TreeViewItem; if (subItem != null) { // Alt TreeViewItem’ı işle ProcessTreeViewItem(subItem, scriptContent); } CheckBox checkBox = obj as CheckBox; if (checkBox != null && checkBox.IsChecked == true) { string script; if (checkBoxScripts.TryGetValue(checkBox, out script)) { // CheckBox’a karşılık gelen batch komutunu sözlükten al ve ekleyebiliriz. scriptContent.AppendLine(script); } } } } private void SaveScriptToFile(string content) { string filePath = "tweaker.bat"; File.WriteAllText(filePath, content); MessageBox.Show(filePath + " kaydedildi."); } }}
- Assistant
Kodunuzdaki mevcut uygulamada,
OneDriveadında birCheckBoxiki kezcheckBoxScriptssözlüğüne eklenmeye çalışılmış, fakat bir dictionary yapısında her anahtar benzersiz olmalıdır. Bunun yerine, herCheckBoxiçin birden fazla komut eklemek istiyorsanız, her anahtar için bir liste veya dizeler dizisi kullanabilirsiniz. Bu senaryodacheckBoxScriptssözlüğünün değerini birstringdizisi (string[]) veya birList<string>yapısına değiştirebilirsiniz.İşte bunu nasıl yapabileceğinize dair güncellenmiş bir kod parçası:
using System.IO; using System.Collections.Generic; using System.Windows; // ... buraya diğer using bildirimlerinizi ekleyin ... namespace ShadesToolkit.Views.Pages { public partial class ExtraDebloat : INavigableView<ExtraDebloatViewModel> { // ... burada önceki kodlarınız ... // CheckBox'lar için string listesi içeren bir sözlük yapısı kullanın. private readonly Dictionary<CheckBox, List<string>> checkBoxScripts = new Dictionary<CheckBox, List<string>>(); public ExtraDebloat(ExtraDebloatViewModel viewModel) { // ... önceki başlatma kodlarınız ... // CheckBox ve karşılık gelen komut listelerini ekleyin. checkBoxScripts.Add(OneDrive, new List<string> { "MinSudo.exe --TrustedInstaller --Verbose --WorkDir=\"\" cmd /c \"del /S /Q \"%InstallDir%\\*OneDrive*\"\"", "MinSudo.exe --TrustedInstaller --Verbose --WorkDir=\"\" cmd /c \"del /S /Q \"%InstallDir%\\*onedrive-backup*\"\"" }); checkBoxScripts.Add(iki, new List<string> { "echo Batch Script 2" }); checkBoxScripts.Add(uc, new List<string> { "echo Batch Script 3" }); checkBoxScripts.Add(dort, new List<string> { "echo Batch Script 4" }); } // ... diğer metodlarınız ... private void ProcessTreeViewItem(ItemsControl itemsControl, StringBuilder scriptContent) { // ... burada önceki kodlarınız ... foreach (object obj in itemsControl.Items) { // ... diğer kontrol yapıları ... CheckBox checkBox = obj as CheckBox; if (checkBox != null && checkBox.IsChecked == true) { List<string> scripts; if (checkBoxScripts.TryGetValue(checkBox, out scripts)) { // CheckBoksa karşılık gelen tüm batch komutlarını sözlükten al ve ekleyin. foreach (string script in scripts) { scriptContent.AppendLine(script); } } } } } // ... burada diğer metodlarınız ... } }Yukarıdaki değişikliklerle, bir
CheckBox'a birden fazla batch komutu ekleyebilir ve seçildiğinde tüm komutlar bat dosyasına eklenecektir. Belirttiğim gibi, herCheckBoxiçin birList<string>olarak değerleri tanımladık veProcessTreeViewItemmetodunda bu listeye eriştikten sonra her bir komutu döngü ilescriptContentiçine ekliyoruz.