-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
128 lines (112 loc) · 4.57 KB
/
Copy pathProgram.cs
File metadata and controls
128 lines (112 loc) · 4.57 KB
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
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
namespace VideoRandomPlayer
{
class Program
{
static readonly string[] VideoExtensions = { ".mp4", ".mkv", ".avi", ".mov", ".wmv", ".flv", ".webm" };
static void Main(string[] args)
{
try
{
// Detect exe location
string folder = AppContext.BaseDirectory;
// Setup cache folder and file
string cacheFolder = Path.Combine(folder, "cache");
if (!Directory.Exists(cacheFolder))
{
Directory.CreateDirectory(cacheFolder);
}
string cacheFile = Path.Combine(cacheFolder, "video_cache.txt");
// Check if refresh is needed
bool needRefresh = true;
string[] files = null;
if (File.Exists(cacheFile))
{
string[] cached = File.ReadAllLines(cacheFile);
if (cached.Length > 1)
{
string cachedHash = cached[0];
string currentHash = GetFolderHash(folder);
if (cachedHash == $"HASH:{currentHash}")
{
needRefresh = false;
files = cached.Skip(1).ToArray();
}
}
}
// Refresh cache if needed
if (needRefresh)
{
var videos = Directory.GetFiles(folder)
.Where(f => VideoExtensions.Contains(Path.GetExtension(f).ToLowerInvariant()))
.ToArray();
if (videos.Length == 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"No video files found in: {folder}");
Console.ResetColor();
Thread.Sleep(2000);
return;
}
string hash = GetFolderHash(folder);
var cacheContent = new string[] { $"HASH:{hash}" }.Concat(videos).ToArray();
File.WriteAllLines(cacheFile, cacheContent);
files = videos;
}
// Play random video
if (files != null && files.Length > 0)
{
Random random = new Random();
string selectedFile = files[random.Next(files.Length)];
// Start the video with default associated application
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = selectedFile,
UseShellExecute = true
};
Process.Start(startInfo);
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("No videos available");
Console.ResetColor();
Thread.Sleep(2000);
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Error: {ex.Message}");
Console.ResetColor();
Thread.Sleep(3000);
}
}
static string GetFolderHash(string folder)
{
var items = Directory.GetFiles(folder)
.Select(f => new FileInfo(f))
.Where(fi => VideoExtensions.Contains(fi.Extension.ToLowerInvariant()))
.OrderBy(fi => fi.Name)
.ToArray();
if (items.Length > 0)
{
string str = string.Join("|", items.Select(fi =>
$"{fi.Name}{fi.Length}{fi.LastWriteTime.Ticks}"));
using (MD5 md5 = MD5.Create())
{
byte[] bytes = Encoding.UTF8.GetBytes(str);
byte[] hash = md5.ComputeHash(bytes);
return BitConverter.ToString(hash);
}
}
return null;
}
}
}