-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHelper.cs
More file actions
70 lines (66 loc) · 2.23 KB
/
Copy pathFileHelper.cs
File metadata and controls
70 lines (66 loc) · 2.23 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
using System;
using System.Data;
using System.IO;
namespace PlaytimeUpdater
{
public static class FileHelper
{
public static int ReadPlaytimeFromTextFile(int line, string filePath)
{
try
{
string[] lines = File.ReadAllLines(filePath);
if (lines.Length > line)
{
return int.Parse(lines[line]);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error reading playtime from text file: {ex.Message}");
}
return 0;
}
public static void SavePlaytimeToTextFile(int value, int line, string filePath)
{
try
{
if ( filePath == MainForm.default_file_path && !File.Exists(filePath))
{
CreateDefaultFile();
}
else if (!File.Exists(filePath))
{
File.CreateText(filePath).Close();
}
string[] lines = File.ReadAllLines(filePath);
if (lines.Length <= line)
{
Array.Resize(ref lines, line + 1);
}
lines[line] = value.ToString();
File.WriteAllLines(filePath, lines);
Console.WriteLine($"Successfully wrote to {filePath.Split('\\')[filePath.Split('\\').Length - 1]}");
}
catch (Exception ex)
{
Console.WriteLine($"Error saving playtime to text file: {ex.Message}");
}
}
public static string GetTextFilePlaytimeDisplay(int line, string filePath)
{
int playtime = ReadPlaytimeFromTextFile(line, filePath); //in seconds
int hours = playtime / 3600;
int minutes = (playtime % 3600) / 60;
return $"File : {hours}h {minutes}m";
}
public static void CreateDefaultFile()
{
if (!Directory.Exists(MainForm.default_text_folder_path))
{
Directory.CreateDirectory(MainForm.default_text_folder_path);
}
File.CreateText(MainForm.default_file_path).Close();
}
}
}