forked from MapStudioProject/MapStudio.UI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecentFileHandler.cs
More file actions
56 lines (48 loc) · 1.95 KB
/
Copy pathRecentFileHandler.cs
File metadata and controls
56 lines (48 loc) · 1.95 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
using System.Linq;
using System.Collections.Generic;
using System.IO;
namespace MapStudio.UI
{
public class RecentFileHandler
{
const int MRUnumber = 6;
public static void LoadRecentList(string filePath, List<string> recentList)
{
recentList.Clear();
if (File.Exists(filePath))
{
StreamReader listToRead = new StreamReader(filePath); //read file stream
string line;
while ((line = listToRead.ReadLine()) != null) //read each line until end of file
{
if ((Directory.Exists(line) || File.Exists(line)) && !recentList.Contains(line))
recentList.Add(line); //insert to list
}
listToRead.Close(); //close the stream
}
}
public static void SaveRecentFile(string recentFile, string filePath, List<string> recentList)
{
if (recentList.Contains(recentFile))
return;
LoadRecentList(filePath, recentList); //load list from file
if (!(recentList.Contains(recentFile))) //prevent duplication on recent list
recentList.Insert(0, recentFile); //insert given path into list
recentList = recentList.Distinct().ToList();
//keep list number not exceeded the given value
while (recentList.Count > MRUnumber) {
recentList.RemoveAt(MRUnumber);
}
//writing menu list to file
//create file called "Recent.txt" located on app folder
StreamWriter stringToWrite =
new StreamWriter(filePath);
foreach (string item in recentList)
{
stringToWrite.WriteLine(item); //write list to stream
}
stringToWrite.Flush(); //write stream to file
stringToWrite.Close(); //close the stream and reclaim memory
}
}
}