-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileManager.cs
100 lines (82 loc) · 3.31 KB
/
FileManager.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml;
using System.Configuration;
namespace Kaiserville_Shop_Randomizer
{
internal class FileManager
{
//string filePath = @"c:\7daystodie\servertools\Shop.xml";
//string filePathDir = @"c:\7daystodie\servertools";
string shopFilePath;
string shopFileDir;
string settingsFilePath;
public FileManager()
{
settingsFilePath = Environment.CurrentDirectory;
shopFileDir = ReturnShopFileUrl(settingsFilePath);
shopFilePath = shopFileDir + "\\Shop.xlm";
Console.WriteLine("Settings file location {0}" , settingsFilePath);
}
public string ReturnShopFileUrl(string path)
{
string temp;
string settingsFile = path + "\\" + "Settings.txt";
if (!File.Exists(settingsFile)) //If the file doesn't exist at the given "Path"
{
//Create a new file
var tempFile = File.Create(settingsFile);
tempFile.Close();//close the file to prevent any errors if access again
using (StreamWriter sw = new StreamWriter(File.Open(settingsFile, FileMode.Truncate)))
{
temp = Environment.CurrentDirectory;
sw.WriteLine(temp); //path for shop xlm
}
Console.WriteLine("To change the Directory where the Shop.xlm file is created, go to the following location {0}.", settingsFile);
Console.WriteLine("This will only run the first time the program is executed, or the file was not found.");
Console.WriteLine("Press any key to continue");
Console.ReadLine();//wait for input from user to continue execution
return temp;
}
else
{
//open the binary reader to get data from file
using (StreamReader sr = new StreamReader(File.Open(settingsFile, FileMode.Open)))
{
temp = sr.ReadLine();
}
}
return temp;
}
public void SavetoXml(List<string> textToSave)
{
if (Directory.Exists(shopFileDir))
{
Console.WriteLine(shopFilePath);
using (StreamWriter sw = new StreamWriter(shopFilePath, false, Encoding.UTF8))
{
sw.WriteLine("<?xlm version=\"1.0\" encoding =\"UTF-8\"?>");
sw.WriteLine("<Shop>");
sw.WriteLine("<ST Version=\"20.2.0\" />");
for (int x = 0; x < textToSave.Count; x++)
{
sw.WriteLine(textToSave[x]);
}
sw.WriteLine("</Shop>");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Save(sw);
Console.WriteLine("saving to file");
}
}
else
{
Console.WriteLine(String.Format("Directory {0} doesn't exist", shopFilePath));
Thread.Sleep(10000);
}
}
}
}