-
Notifications
You must be signed in to change notification settings - Fork 1
/
Form1.cs
789 lines (662 loc) · 29.8 KB
/
Form1.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
using AngleSharp.Media;
using System.Diagnostics.Metrics;
using System.Text;
using YoutubeExplode;
using YoutubeExplode.Common;
using YoutubeExplode.Videos.Streams;
using System.Windows.Forms;
using TagLib;
using System.Diagnostics;
using Microsoft.VisualBasic;
using Fastenshtein;
using File = System.IO.File;
using static System.Windows.Forms.LinkLabel;
namespace YTPD
{
public partial class Form1 : Form
{
public double prog = 0;
public static bool isConverting = false;
public static bool isPaused = false;
public static bool isSaving = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// set up saved data
txt_Dir.Text = Properties.Settings.Default.Directory;
LoadCSVIntoDataGridView();
}
private async void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
var youtube = new YoutubeClient();
string album = "";
string thumb = "";
// You can specify either the video URL or its ID
try
{
var albuminfo = await youtube.Playlists.GetAsync(txt_URL.Text);
album = albuminfo.Title;
thumb = albuminfo.Thumbnails[1].Url;
}
catch
{
MessageBox.Show("Unable to add album. Please copy/paste again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
button1.Enabled = true;
return;
}
album = album.Replace("Album - ", "");
album = album.Replace("- Topic", "");
// Get all playlist videos
Int16 songnum = 1;
await foreach (var video in youtube.Playlists.GetVideosAsync(txt_URL.Text))
{
var title = video.Title;
var band = video.Author.ChannelTitle;
var author = video.Author;
var duration = video.Duration;
var link = video.Url;
band = band.Replace("- Topic", "").Trim();
dgv_downloads.Rows.Add(band, album, songnum.ToString(), title, duration, link, "0", "0", "No");
songnum++;
}
dgv_downloads.Refresh();
txt_URL.Clear();
txt_URL.Focus();
SaveDataGridViewToCSV();
button1.Enabled = true;
}
private async void timer1_Tick(object sender, EventArgs e)
{
// ensure a directory exists
if (!Directory.Exists(txt_Dir.Text)) Directory.CreateDirectory(txt_Dir.Text);
// turn it off while it's running
timer1.Enabled = false;
// turn it off while it's running
timer1.Enabled = false;
string artist = "";
string album = "";
string songnum = "";
string song = "";
string duration = "";
string link = "";
foreach (DataGridViewRow row in dgv_downloads.Rows)
{
// if the user paused downloading, then go away
if (isPaused) return;
if (row.Cells[0].Value == null || row.Cells[0].Value.ToString().Length < 1)
{
try
{
dgv_downloads.Rows.RemoveAt(row.Index);
}
catch
{
break;
}
continue;
}
if (row.Cells[0].Value.ToString().ToLower().Contains("romeo"))
{
song = "";
}
Int16 dlpercent = Convert.ToInt16(row.Cells["DL"].Value);
if (dlpercent > 0 && dlpercent < 100) return;
if (dlpercent == 0)
{
artist = row.Cells["Artist"].Value.ToString();
album = row.Cells["Album"].Value.ToString();
songnum = row.Cells["SongNum"].Value.ToString();
song = row.Cells["Song"].Value.ToString();
duration = row.Cells["Duration"].Value.ToString();
link = row.Cells["Link"].Value.ToString();
row.Cells["DL"].Value = "1";
// create artist directory
if (!Directory.Exists(txt_Dir.Text + "\\" + artist))
{
Directory.CreateDirectory(txt_Dir.Text + "\\" + GetValidFilename(artist));
}
// create album directory
if (!Directory.Exists(txt_Dir.Text + "\\" + artist + "\\" + album))
{
Directory.CreateDirectory(txt_Dir.Text + "\\" + GetValidFilename(artist) + "\\" + GetValidFilename(album));
}
// highlight the row being used
dgv_downloads.Rows[row.Index].DefaultCellStyle.BackColor = Color.SteelBlue;
dgv_downloads.Rows[row.Index].DefaultCellStyle.ForeColor = Color.Azure;
dgv_downloads.FirstDisplayedScrollingRowIndex = row.Index;
// get the manifest information
string fullpath = "";
try
{
var youtube = new YoutubeClient();
var streamManifest = await youtube.Videos.Streams.GetManifestAsync(link);
var streamInfo = streamManifest.GetAudioOnlyStreams().GetWithHighestBitrate();
// set the path
fullpath = Path.Combine(txt_Dir.Text + "\\" + GetValidFilename(artist) + "\\" + GetValidFilename(album), songnum + " - " + GetValidFilename(song) + "." + streamInfo.Container);
if (System.IO.File.Exists(fullpath))
{
row.Cells["DL"].Value = "100";
break;
}
var progress = new Progress<double>(percentage =>
{
row.Cells["DL"].Value = Math.Round(percentage * 100, 0);
});
// actual stream
var stream = await youtube.Videos.Streams.GetAsync(streamInfo);
await youtube.Videos.Streams.DownloadAsync(streamInfo, fullpath, progress);
dgv_downloads.Rows[row.Index].DefaultCellStyle.BackColor = Color.Azure;
dgv_downloads.Rows[row.Index].DefaultCellStyle.ForeColor = Color.Black;
}
catch
{
row.Cells["DL"].Value = "100";
dgv_downloads.Rows[row.Index].DefaultCellStyle.BackColor = Color.DarkRed;
dgv_downloads.Rows[row.Index].DefaultCellStyle.ForeColor = Color.White;
}
finally
{
if (!isConverting && fullpath.Length > 1)
{
isConverting = true;
await ConvertFile(fullpath, fullpath.Substring(fullpath.LastIndexOf('.')));
AppendToFile(fullpath);
isConverting = false;
}
}
SaveDataGridViewToCSV();
break;
}
}
// turn timer back on when done
timer1.Enabled = true;
}
static async Task ConvertFile(string inputFilePath, string fileExt)
{
string outputFilePath = inputFilePath.Replace(fileExt, ".mp3");
string ffmpegcom = $"-n -i \"{inputFilePath}\" \"{outputFilePath}\"";
int exitCode = 1;
// Setup parameters
ProcessStartInfo psi = new ProcessStartInfo()
{
FileName = "ffmpeg",
Arguments = ffmpegcom,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
// Start the process
using (Process process = new Process { StartInfo = psi })
{
process.Start();
// Read the output and error streams
string error = process.StandardError.ReadToEnd();
// Wait for the process to exit
await Task.Run(() => process.WaitForExit(1200000));
// Display the errors
AppendToFile("FFmpeg Errors:");
AppendToFile(error);
// Check the exit code
exitCode = process.ExitCode;
// process errors that can skip to exit
if (error.Contains("No such file") || error.Contains("Invalid data") || error.Contains("already exists") || error.Contains("not in a correct format")) exitCode = 0;
// file existence errors that can be skipped
if (!System.IO.File.Exists(outputFilePath) || !System.IO.File.Exists(inputFilePath)) exitCode = 0;
while (process.HasExited == true || exitCode == 0)
{
if (process.HasExited == true)
{
break;
} else
{
await Task.Delay(1000);
AppendToFile("Waiting for process...");
}
}
}
// wait to delete file
Int16 waitcounter = 0;
while (true)
{
waitcounter += 1;
try
{
using (FileStream fileStream = File.OpenWrite(inputFilePath))
{
break;
}
}
catch (IOException)
{
if (waitcounter > 5)
{
AppendToFile("I can't close the file. Giving up.");
return;
}
// File is still locked or in use by another process
AppendToFile("File is locked. Waiting for it to become available...");
Thread.Sleep(60000); // Wait for 1 minute before retrying
}
}
// Notify file deletion
AppendToFile("Exit code received, deleting original file.");
await Task.Run(() => System.IO.File.Delete(inputFilePath));
}
private void timer_tag_Tick(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dgv_downloads.Rows)
{
// setup variables
bool FoundFile = false;
string[] artist = new[] { "" };
string album = "";
string songnum = "";
string song = "";
string duration = "";
string link = "";
string fullpath = "";
if (row.Cells[0].Value == null || row.Cells[0].Value == "") return;
Int16 dlpercent = Convert.ToInt16(row.Cells["DL"].Value);
if (dlpercent == 100 && row.Cells["Tagged"].Value.ToString() == "0")
{
artist = new[] { row.Cells["Artist"].Value.ToString() };
album = row.Cells["Album"].Value.ToString();
songnum = row.Cells["SongNum"].Value.ToString();
song = row.Cells["Song"].Value.ToString();
duration = row.Cells["Duration"].Value.ToString();
link = row.Cells["Link"].Value.ToString();
// actual stream
fullpath = Path.Combine(txt_Dir.Text + "\\" + GetValidFilename(row.Cells["Artist"].Value.ToString()) + "\\" + GetValidFilename(album), songnum + " - " + GetValidFilename(song) + ".mp3");
// does the file exist? If not, marked it tagged as the user probably moved it already
if (!System.IO.File.Exists(fullpath))
{
continue;
} else {
FoundFile = true;
}
TagFile(fullpath, artist, album, song, link, songnum);
row.Cells["Tagged"].Value = "1";
row.Cells["Converted"].Value = "Yes";
}
else if (dlpercent == 100 && row.Cells["Tagged"].Value.ToString() == "1")
{
row.Cells["Converted"].Value = "Yes";
}
}
}
private void TagFile(string fullpath, string[] artist, string album, string song, string link, string songnum)
{
// check if file is in use
bool fileInUse = true;
while (fileInUse)
{
try
{
// Attempt to open the file with FileShare.None to check if it's in use
using (var fileStream = new FileStream(fullpath, FileMode.Open, FileAccess.Read, FileShare.None))
{
// if the file can be opened, then let's write the mp3 tags
fileInUse = false;
}
}
catch (IOException)
{
// File is still in use, wait for a short duration before trying again
AppendToFile(fullpath + " in use. Waiting to tag...");
continue;
}
}
var tagfile = TagLib.File.Create(fullpath);
tagfile.Tag.AlbumArtists = artist;
tagfile.Tag.Artists = artist;
tagfile.Tag.Album = album;
tagfile.Tag.Title = song;
tagfile.Tag.Comment = link;
tagfile.Tag.Track = Convert.ToUInt16(songnum);
tagfile.Save();
AppendToFile(fullpath + " successfully tagged with ID3. " + artist.ToString() + " > " + album + " > " + songnum + " > " + song);
}
private void SaveDataGridViewToCSV()
{
if (isSaving == true) return; //no sense saving if it's already trying
string filePath = Application.StartupPath + "\\grid.dat";
isSaving = true;
try
{
// Create the CSV file and write the header
using (var streamWriter = new StreamWriter(filePath, false, Encoding.UTF8))
{
// Write header
for (int i = 0; i < dgv_downloads.Columns.Count; i++)
{
streamWriter.Write(dgv_downloads.Columns[i].HeaderText);
if (i < dgv_downloads.Columns.Count - 1)
streamWriter.Write("|");
}
streamWriter.WriteLine();
// Write data
for (int i = 0; i < dgv_downloads.Rows.Count; i++)
{
for (int j = 0; j < dgv_downloads.Columns.Count; j++)
{
streamWriter.Write(dgv_downloads.Rows[i].Cells[j].Value);
if (j < dgv_downloads.Columns.Count - 1)
streamWriter.Write("|");
}
streamWriter.WriteLine();
}
}
AppendToFile($"Data saved to {filePath}");
}
catch (Exception ex)
{
AppendToFile($"Error saving data: {ex.Message}");
}
finally
{
isSaving = false;
}
}
private void LoadCSVIntoDataGridView()
{
string filePath = Application.StartupPath + "\\grid.dat";
dgv_downloads.Rows.Clear();
if (!System.IO.File.Exists(filePath)) return;
try
{
// Read all lines from the CSV file
string[] lines = System.IO.File.ReadAllLines(filePath, Encoding.UTF8);
// Add rows to the DataGridView
for (int i = 1; i < lines.Length; i++)
{
string[] values = lines[i].Split('|');
// Create a new row
int rowIndex = dgv_downloads.Rows.Add();
// Set cell values for the row
for (int j = 0; j < values.Length; j++)
{
dgv_downloads.Rows[rowIndex].Cells[j].Value = values[j];
}
// if the DLPercent is not 100 or 0, then restart the download
Int16 dlpercent = Convert.ToInt16(dgv_downloads.Rows[rowIndex].Cells["DL"].Value.ToString());
if (dlpercent > 0 && dlpercent < 100) dgv_downloads.Rows[rowIndex].Cells["DL"].Value = "0";
}
dgv_downloads.Refresh();
AppendToFile($"Data loaded from {filePath}");
}
catch (Exception ex)
{
AppendToFile($"Error loading data: {ex.Message}");
}
}
private void dgv_downloads_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void timer_save_Tick(object sender, EventArgs e)
{
SaveDataGridViewToCSV();
}
private void button2_Click(object sender, EventArgs e)
{
using (var folderBrowserDialog = new FolderBrowserDialog())
{
// Set the initial folder if needed
// folderBrowserDialog.SelectedPath = "C:\\";
// Set the title of the dialog
folderBrowserDialog.Description = "Select a download folder";
// Show the dialog and get the result
DialogResult result = folderBrowserDialog.ShowDialog();
// Check if the user clicked OK
if (result == DialogResult.OK)
{
// Get the selected folder path
string selectedFolder = folderBrowserDialog.SelectedPath;
txt_Dir.Text = selectedFolder;
Properties.Settings.Default.Directory = selectedFolder;
Properties.Settings.Default.Save();
}
else
{
// Handle the case where the user canceled the dialog
AppendToFile("Folder selection canceled by the user.");
}
}
}
private void txt_URL_TextChanged(object sender, EventArgs e)
{
}
private async void timer_convert_Tick(object sender, EventArgs e)
{
if (!Directory.Exists(txt_Dir.Text)) return;
// Get subdirectories
string[] subdirectories = Directory.GetDirectories(txt_Dir.Text);
// Get all files with a ".webm" extension in subdirectories
string[] nonMp3Files = Directory.EnumerateFiles(txt_Dir.Text, "*", SearchOption.AllDirectories)
.Where(file => !file.EndsWith(".mp3", StringComparison.OrdinalIgnoreCase))
.ToArray();
AppendToFile("\nNon-mp3 files files:" + nonMp3Files.Length);
// converts non-mp3 to mp3
foreach (string nonMp3File in nonMp3Files)
{
foreach (DataGridViewRow row in dgv_downloads.Rows)
{
if (row.Cells[0].Value == null || row.Cells[0].Value.ToString().Length == 0 || row.Cells["DL"].Value.ToString() != "100") continue;
// review levenshtein distance for mp3 file and song name so we only convert the mp3
string foundsong = nonMp3File.Substring(nonMp3File.LastIndexOf('\\') + 2);
foundsong = foundsong.Substring(foundsong.IndexOf('-') + 2, foundsong.LastIndexOf('.') - 4);
string cellsong = row.Cells["Song"].Value.ToString();
int lev = Levenshtein.Distance(foundsong, cellsong);
// if threshold is met, then convert non-mp3 to mp3
if (lev > 70)
{
await ConvertFile(nonMp3File, nonMp3File.Substring(nonMp3File.LastIndexOf('.')));
Console.WriteLine(nonMp3File);
}
bool SongFound = false;
foreach (DataGridViewRow row in dgv_downloads.Rows)
{
if (row.Cells[0].Value == null || row.Cells[0].Value.ToString().Length == 0) continue;
// review levenshtein distance for mp3 file and song name so we only convert the mp3
string foundsong = nonMp3File.Substring(nonMp3File.LastIndexOf('\\') + 2);
foundsong = foundsong.Substring(foundsong.IndexOf('-') + 2, foundsong.LastIndexOf('.') - 4);
string cellsong = row.Cells["Song"].Value.ToString();
int lev = Levenshtein.Distance(foundsong, cellsong);
// if threshold is met, then convert non-mp3 to mp3
if (lev < 5)
{
AppendToFile("Levenshtein threshold passed [" + lev + "] - " + nonMp3File);
await ConvertFile(nonMp3File, nonMp3File.Substring(nonMp3File.LastIndexOf('.')));
SongFound = true;
break;
}
}
if(SongFound == false)
{
AppendToFile("Unable to find [" + nonMp3File + "'] in your downloaded songs. Attempting convert anyways...");
await ConvertFile(nonMp3File, nonMp3File.Substring(nonMp3File.LastIndexOf('.')));
}
}
}
private string GetValidFilename(string inputFilename)
{
// Get invalid characters in a filename
char[] invalidChars = Path.GetInvalidFileNameChars();
// Replace invalid characters with an underscore
string validFilename = new string(inputFilename.Select(c => invalidChars.Contains(c) ? '_' : c).ToArray());
return validFilename;
}
private void btn_Pause_Click(object sender, EventArgs e)
{
isPaused = true;
timer1.Enabled = false;
timer_convert.Enabled = false;
timer_tag.Enabled = false;
btn_Pause.Visible = false;
btn_Resume.Visible = true;
}
private void btn_Resume_Click(object sender, EventArgs e)
{
isPaused = false;
timer1.Enabled = true;
timer_convert.Enabled = true;
timer_tag.Enabled = true;
btn_Pause.Visible = true;
btn_Resume.Visible = false;
}
private async void button3_Click(object sender, EventArgs e)
{
}
private async void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (isSaving != false)
{
e.Cancel = true;
isPaused = true;
timer1.Enabled = false;
btn_Pause.Visible = false;
btn_Resume.Visible = true;
MessageBox.Show("Your progress is saving. I will pause progress and attempt to close again in [5] seconds.", "Close Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
// Use Task.Run to avoid marking the FormClosing event handler as async
await Task.Run(async () =>
{
// Introduce a CancellationToken for cleanup
using (var cancellationTokenSource = new CancellationTokenSource())
{
// Allow cancellation after 5 seconds
await Task.Delay(5000, cancellationTokenSource.Token);
// Ensure the cancellation is not already requested before closing
if (!cancellationTokenSource.Token.IsCancellationRequested)
{
// Close the form
BeginInvoke(new Action(() => Close()));
}
}
});
}
}
private void dgv_downloads_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
// ensure something is actually selected
if (e.RowIndex >= 0 && e.RowIndex < dgv_downloads.Rows.Count)
{
int rowIndex = e.RowIndex;
// get the selected band name
string bandname = dgv_downloads.Rows[rowIndex].Cells[0].Value.ToString();
// correct the band name
string userInput = Interaction.InputBox("If the band name is incorrect, this is your chance to update it:", "YTAD", bandname);
// if null then ignore
if (userInput != null && userInput.Length > 1)
{
// update band name for each row with the wrong band name
foreach (DataGridViewRow row in dgv_downloads.Rows)
{
// no null cells!
if (row.Cells[0].Value != null && row.Cells[0].Value.ToString().Length > 1)
{
// if the band name is the same as the one that needs correcting then correct it
if (row.Cells[0].Value.ToString() == bandname)
{
row.Cells[0].Value = userInput;
}
}
}
// refresh table
dgv_downloads.Refresh();
// save
SaveDataGridViewToCSV();
}
}
}
private void btn_secret_MouseDoubleClick(object sender, MouseEventArgs e)
{
}
private void dgv_downloads_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
}
}
private void menu_cleardata_Click(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show("Are you sure want to delete your data?", "YouTube Album Downloader", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (dr == DialogResult.Yes)
{
dgv_downloads.Rows.Clear();
SaveDataGridViewToCSV();
}
}
private void btn_secret_Click(object sender, EventArgs e)
{
}
private void openDataFileToolStripMenuItem_Click(object sender, EventArgs e)
{
Process.Start("explorer.exe", Application.StartupPath);
}
private void RestartBadItems_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dgv_downloads.Rows)
{
if (row.Cells[0].Value == null || row.Cells[0].Value.ToString().Length == 0) continue;
if (row.Cells["Converted"].Value.ToString() == "No")
{
row.Cells["DL"].Value = "0";
}
}
}
private void timer_count_Tick(object sender, EventArgs e)
{
Int32 NotStarted = 0;
Int32 Downloaded = 0;
Int32 Failed = 0;
Int32 Completed = 0;
foreach (DataGridViewRow row in dgv_downloads.Rows)
{
if (row.Cells[0].Value != null && row.Cells[0].Value.ToString().Length > 1)
{
try
{
if (row.Cells["DL"].Value.ToString() == "0") NotStarted++;
if (row.Cells["DL"].Value.ToString() == "100") Downloaded++;
if (row.Cells["DL"].Value.ToString() == "100" && row.Cells["Converted"].Value.ToString() == "No") Failed++;
if (row.Cells["DL"].Value.ToString() == "100" && row.Cells["Tagged"].Value.ToString() == "1") Completed++;
}
catch
{
break;
}
continue;
}
}
lbl_status.Text = "Not Started: " + NotStarted.ToString() + " � Downloaded: " + Downloaded.ToString() + " � Failed: " + Failed.ToString() + " � Completed: " + Completed.ToString();
}
public static void AppendToFile(string content)
{
string filePath = Application.StartupPath + "\\error.log";
if(!System.IO.File.Exists(filePath))
System.IO.File.Create(filePath);
// Get the current date and time
string dateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
// Create the log entry with the date, time, and content
string logEntry = $"{dateTime} - {content}{Environment.NewLine}";
try
{
// Append the log entry to the file using StreamWriter
using (StreamWriter writer = File.AppendText(filePath))
{
writer.Write(logEntry);
Console.WriteLine("Log entry appended to the file successfully.");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error appending to the file: {ex.Message}");
}
}
}
}