-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
174 lines (157 loc) · 6.89 KB
/
MainWindow.xaml.cs
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
namespace DuyuruUygulamasi
{
public partial class MainWindow : Window
{
private string dosyaAdi = "duyurular.txt";
private string ftpSunucu = "ftp://ftpadresiniz/htdocs/";
private string ftpKullaniciAdi = "kullanıcıadınız";
private string ftpSifre = "şifreniz";
private bool ilkCalistirma = true;
private bool pencereGizli = true;
public MainWindow()
{
InitializeComponent();
Thread thread = new Thread(new ThreadStart(MetniKontrolEt));
thread.IsBackground = true;
thread.Start();
// Tarih ve saat bilgilerini güncelle
TarihSaatGuncelle();
}
private void MetniKontrolEt()
{
while (true)
{
try
{
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(ftpSunucu + dosyaAdi);
ftpRequest.Credentials = new NetworkCredential(ftpKullaniciAdi, ftpSifre);
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
using (FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse())
using (Stream ftpStream = ftpResponse.GetResponseStream())
using (StreamReader reader = new StreamReader(ftpStream))
{
string[] satirlar = reader.ReadToEnd().Split('\n');
if (satirlar.Length > 0 && !string.IsNullOrWhiteSpace(satirlar[0]))
{
for (int i = 0; i < satirlar.Length; i++)
{
string satir = satirlar[i].Trim();
string[] parcalar = satir.Split('$');
if (parcalar.Length == 2)
{
string duyuruMetni = parcalar[0];
int sure;
if (int.TryParse(parcalar[1], out sure))
{
Dispatcher.Invoke(() =>
{
DuyuruMetni.Text = duyuruMetni;
DuyuruMetni.Visibility = Visibility.Visible;
pencereGizli = false;
this.Show();
this.Activate();
});
Thread.Sleep(sure * 1000);
Dispatcher.Invoke(() =>
{
DuyuruMetni.Visibility = Visibility.Hidden;
pencereGizli = true;
this.Hide();
});
SilSatir(ftpSunucu + dosyaAdi, i);
}
}
}
}
else
{
Dispatcher.Invoke(() =>
{
this.Hide();
pencereGizli = true;
});
}
}
ilkCalistirma = false;
}
catch (Exception ex)
{
MessageBox.Show("Hata: " + ex.Message);
}
Thread.Sleep(1000); // 1 saniye beklet
}
}
private void SilSatir(string ftpDosyaAdresi, int satirIndex)
{
try
{
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(ftpDosyaAdresi);
ftpRequest.Credentials = new NetworkCredential(ftpKullaniciAdi, ftpSifre);
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
using (FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse())
using (Stream ftpStream = ftpResponse.GetResponseStream())
using (StreamReader reader = new StreamReader(ftpStream))
{
string[] satirlar = reader.ReadToEnd().Split('\n');
StringBuilder yeniIcerik = new StringBuilder();
for (int i = 0; i < satirlar.Length; i++)
{
if (i != satirIndex)
{
yeniIcerik.AppendLine(satirlar[i]);
}
}
FtpWebRequest ftpWriteRequest = (FtpWebRequest)WebRequest.Create(ftpDosyaAdresi);
ftpWriteRequest.Credentials = new NetworkCredential(ftpKullaniciAdi, ftpSifre);
ftpWriteRequest.Method = WebRequestMethods.Ftp.UploadFile;
byte[] byteArray = Encoding.UTF8.GetBytes(yeniIcerik.ToString());
ftpWriteRequest.ContentLength = byteArray.Length;
using (Stream requestStream = ftpWriteRequest.GetRequestStream())
{
requestStream.Write(byteArray, 0, byteArray.Length);
}
FtpWebResponse response = (FtpWebResponse)ftpWriteRequest.GetResponse();
response.Close();
}
Dispatcher.Invoke(() =>
{
if (pencereGizli)
{
this.Hide();
}
});
if (!pencereGizli)
{
Dispatcher.Invoke(() =>
{
this.Show();
pencereGizli = true;
});
}
}
catch (Exception ex)
{
MessageBox.Show("Hata: " + ex.Message);
}
}
private void TarihSaatGuncelle()
{
DispatcherTimer timer = new DispatcherTimer();
timer.Tick += Timer_Tick;
timer.Interval = TimeSpan.FromSeconds(1);
timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
TarihTextBlock.Text = DateTime.Now.ToString("dd/MM/yyyy");
SaatTextBlock.Text = DateTime.Now.ToString("HH:mm:ss");
}
}
}