using Microsoft.Win32;
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 System.Xml.Serialization;
namespace WpfDosyaCalistirma
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
string kaydedilecekDosyaYolu = System.AppDomain.CurrentDomain.BaseDirectory + "\\dosyaAdlari.xml";
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
try
{
//Uygulamadan çıkarken kaydettiğimiz dosya adlarını listboxa ekleyelim
List<string> dosyaAdlari = new List<string>();
using (System.IO.StreamReader okuyucu = new System.IO.StreamReader(kaydedilecekDosyaYolu))
{
XmlSerializer deSerilestirici = new XmlSerializer(typeof(List<string>));
dosyaAdlari = (List<string>)deSerilestirici.Deserialize(okuyucu);
}
for (int i = 0; i < dosyaAdlari.Count; i++)
{
listBox1.Items.Add(dosyaAdlari[i]);
}
}
catch
{
}
}
private void btnDosyaEkle_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog yeniDialog = new OpenFileDialog();
if (yeniDialog.ShowDialog().Value == true)
{
if (!String.IsNullOrWhiteSpace(yeniDialog.FileName))
{
listBox1.Items.Add(yeniDialog.FileName);
}
}
}
private void btnCalistir_Click(object sender, RoutedEventArgs e)
{
var secilenDosya = listBox1.SelectedItem as string;
if (secilenDosya != null)
{
try
{
System.Diagnostics.Process.Start(secilenDosya);
}
catch
{
MessageBox.Show("Dosya çalıştırılırken bir hata oluştu.\nDosyayı yeniden çalıştırmayı deneyiniz.");
}
}
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//listboxdaki tüm dosya adlarını kaydetmek için almalıyız.
List<string> dosyaAdlari = new List<string>();
for (int i = 0; i < listBox1.Items.Count; i++)
{
dosyaAdlari.Add(listBox1.Items[i].ToString());
}
//Çıkışta dosyaları xmle kaydedelim
using (System.IO.StreamWriter yazici = new System.IO.StreamWriter(kaydedilecekDosyaYolu))
{
XmlSerializer serilestirici = new XmlSerializer(typeof(List<string>));
serilestirici.Serialize(yazici, dosyaAdlari);
}
}
private void listBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var secilenDosya = listBox1.SelectedItem as string;
if (secilenDosya != null)
{
try
{
System.Diagnostics.Process.Start(secilenDosya);
}
catch
{
MessageBox.Show("Dosya çalıştırılırken bir hata oluştu.\nDosyayı yeniden çalıştırmayı deneyiniz.");
}
}
}
}
}