Şu kodlarımda BackClick metodunda $OEM$ Klasörünü silsin using System.IO; using…
- User
Şu kodlarımda Back_Click metodunda Klasörünü silsin 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 System.Xml.Linq; using System.Xml; using System.Windows.Controls; using System.Windows.Media.Imaging;
namespace ShadesToolkit.Views.Pages { public partial class AutoUnattend : INavigableView {
private string MountFolder = Path.Combine(Path.GetTempPath(), "MountFolder"); public ObservableCollection<Command> Commands { get; set; } public AutoUnattendViewModel ViewModel { get; } public AutoUnattend(AutoUnattendViewModel viewModel) { ViewModel = viewModel; DataContext = this; InitializeComponent(); Commands = new ObservableCollection<Command>(); dataGrid.ItemsSource = Commands; } private Dictionary<string, string> parameterMappings = new Dictionary<string, string> { {"MBSetup.exe","/VERYSILENT /SUPPRESSMSGBOXES /NORESTART" }, {"SpotifySetup.exe","/silent" }, {"7z.exe","/s" } }; private void AddApplication_Click(object sender, RoutedEventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Multiselect = true; if (openFileDialog.ShowDialog() == true) { foreach (var fileName in openFileDialog.FileNames) { var icon = GetIcon(fileName); var parameters = GetParametersForFile(fileName); Commands.Add(new Command { ExecutionQueue = fileName, Type = "Installer", Icon = icon, Parameters = parameters }); } } } private string GetParametersForFile(string fileName) { fileName = fileName.ToLower(); foreach (var entry in parameterMappings) { if (fileName.Contains(entry.Key.ToLower())) { // Eğer dosya adı .msi ile bitiyorsa ve sözlükte belirtilen bir parametresi varsa, belirtilen parametreleri döndür if (fileName.EndsWith(".msi")) { return entry.Value; } return entry.Value; } } // Eğer dosya adı .msi ile bitiyorsa ve sözlükte belirtilen bir parametresi yoksa, belirtilen parametreleri döndür if (fileName.EndsWith(".msi")) { return "/i \"%WINDIR%\\Setup\\Files\\" + Path.GetFileName(fileName) + "\" /qn"; } return string.Empty; // Varsayılan parametreler yoksa boş döndür } private void AddCommand_Click(object sender, RoutedEventArgs e) { Commands.Add(new Command { Type = "Command" }); } private void Save_Click(object sender, RoutedEventArgs e) { // DismApi'yi başlat DismApi.Initialize(DismLogLevel.LogErrorsWarnings, "log.txt"); DismSession session = DismApi.OpenOfflineSession(MountFolder); DismMountedImageInfoCollection mountedImages = DismApi.GetMountedImages(); if (mountedImages.Any(image => image.MountPath == MountFolder)) { } else { // Eğer MountFolder monte edilmediyse, bir uyarı mesajı göster MessageBox.Show("Lütfen önce bir sistem görüntüsü monte edin.", "Uyarı", System.Windows.MessageBoxButton.OK, MessageBoxImage.Information); return; } // Monte edilmiş WIM dosyasının yolunu al string wimFilePath = DismApi.GetMountedImages().FirstOrDefault()?.ImageFilePath; // WIM dosyasının bulunduğu dizini al string wimDirectory = Path.GetDirectoryName(wimFilePath); string setupPath = Path.Combine(wimDirectory, "$OEM$\\$$\\Setup"); string scriptsPath = Path.Combine(setupPath, "Scripts"); string filesPath = Path.Combine(setupPath, "Files"); // 'Setup' klasörünün var olup olmadığını kontrol edin, yoksa oluşturun if (!Directory.Exists(setupPath)) { Directory.CreateDirectory(setupPath); } // 'Scripts' ve 'Files' klasörlerinin var olup olmadığını kontrol edin, yoksa oluşturun if (!Directory.Exists(scriptsPath)) { Directory.CreateDirectory(scriptsPath); } if (!Directory.Exists(filesPath)) { Directory.CreateDirectory(filesPath); } string cmdPath = Path.Combine(scriptsPath, "SetupComplete.cmd"); if (File.Exists(cmdPath)) { List<string> lines = new List<string>(File.ReadAllLines(cmdPath)); for (int i = 0; i < lines.Count; i++) { foreach (var command in Commands) { string sourceFile = command.ExecutionQueue; string fileName = Path.GetFileName(sourceFile); if (fileName != null && lines[i].Contains(fileName)) // fileName null kontrolü ekledik { lines.RemoveAt(i); i--; } } } using (StreamWriter sw = new StreamWriter(cmdPath, false)) // false, dosyanın üzerine yazılmasını sağlar { foreach (string line in lines) { sw.WriteLine(line); } } using (StreamWriter sw = new StreamWriter(cmdPath)) { for (int i = 0; i < lines.Count; i++) { // Son iki satırdan bir önceki satıra geldiğimizde yeni verileri ekleyin if (i == lines.Count - 2) { foreach (var command in Commands) { string sourceFile = command.ExecutionQueue; string fileName = Path.GetFileName(sourceFile); if (File.Exists(sourceFile)) { // Dosya varsa, kopyalayın string destFile = Path.Combine(filesPath, fileName); File.Copy(sourceFile, destFile, true); // .exe veya .msi dosyalarının başına belirtilen yolu ekleyin if (fileName.EndsWith(".exe")) { sw.WriteLine($"\"%WINDIR%\\Setup\\Files\\{fileName}\" {command.Parameters}"); } else if (fileName.EndsWith(".msi")) { sw.WriteLine($"MsiExec.exe /i \"%WINDIR%\\Setup\\Files\\{fileName}\" {command.Parameters}"); } } else { // Dosya yoksa, doğrudan CMD dosyasına ekleyin sw.WriteLine($"{sourceFile} {command.Parameters}"); } } } sw.WriteLine(lines[i]); } } MessageBox.Show("İşlem başarıyla tamamlandı!"); try { DismApi.CloseSession(session); } catch { } try { DismApi.Shutdown(); } catch { } } else { using (StreamWriter sw = new StreamWriter(Path.Combine(scriptsPath, "SetupComplete.cmd"), true)) { // Bat dosyasının başına '@echo off' ekleyin sw.WriteLine("@echo off"); // Tüm dosyaları kopyalayın veya CMD dosyasına ekleyin foreach (var command in Commands) { string sourceFile = command.ExecutionQueue; string fileName = Path.GetFileName(sourceFile); if (File.Exists(sourceFile)) { // Dosya varsa, kopyalayın string destFile = Path.Combine(filesPath, fileName); File.Copy(sourceFile, destFile, true); // .exe veya .msi dosyalarının başına belirtilen yolu ekleyin if (fileName.EndsWith(".exe")) { sw.WriteLine($"\"%WINDIR%\\Setup\\Files\\{fileName}\" {command.Parameters}"); } else if (fileName.EndsWith(".msi")) { sw.WriteLine($"MsiExec.exe /i \"%WINDIR%\\Setup\\Files\\{fileName}\" {command.Parameters}"); } } else { // Dosya yoksa, doğrudan CMD dosyasına ekleyin sw.WriteLine($"{sourceFile} {command.Parameters}"); } } // Bat dosyasının sonuna belirtilen kodları ekleyin sw.WriteLine("rd /q /s \"%WINDIR%\\Setup\\Files\""); sw.WriteLine("del /q /f \"%0\""); } MessageBox.Show("İşlem başarıyla tamamlandı!"); // DismApi'yi kapat DismApi.Shutdown(); } } private BitmapImage GetIcon(string filePath) { var icon = System.Drawing.Icon.ExtractAssociatedIcon(filePath); var bitmap = icon.ToBitmap(); var stream = new MemoryStream(); bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png); var bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = new MemoryStream(stream.ToArray()); bitmapImage.EndInit(); return bitmapImage; } private void Back_Click(object sender, RoutedEventArgs e) { } } public class Command { public string ExecutionQueue { get; set; } public string Parameters { get; set; } public string Type { get; set; } public BitmapImage Icon { get; set; } } - Assistant