Текстовый файл с переменными - C#
Формулировка задачи:
Необходимо создать текстовый файл с переменными, что бы после того, как запихну программу в .exe была возможность менять некие параметры. Нужно именно .txt файл.
Нашел такой вот кусок кода, это считывание из .txt, а как присвоить это переменным?
class ReadFromFile
{
static void Main()
{
// Read the file as one string.
string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt");
System.Console.WriteLine("Contents of WriteText.txt = {0}", text);
// Read each line of the file into a string array. Each element
// of the array is one line of the file.
string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Public\TestFolder\WriteLines2.txt");
// Display the file contents by using a foreach loop.
System.Console.WriteLine("Contents of WriteLines2.txt = ");
foreach (string line in lines)
{
// Use a tab to indent each line of the file.
Console.WriteLine("\t" + line);
}Решение задачи: «Текстовый файл с переменными»
textual
Листинг программы
// Параметры файла, куда сохраняем.
string path = "1.txt"; // путь.
char separator = ','; // символ-разделитель (может быть массив).
// Настройки для сохранения.
int a = 5;
string b = "London";
// Пишем
StreamWriter streamWriter1 = new StreamWriter(path);
using (streamWriter1)
{
streamWriter1.WriteLine(a.ToString() + separator + b);
}
// Сбили настройки, для проверки.
a = 8;
b = "Москва";
// Читаем
StreamReader streamReader1 = new StreamReader(path);
using (streamReader1)
{
string buffStr = streamReader1.ReadLine();
if (buffStr != null)
{
string[] buff = buffStr.Split(separator);
a = int.Parse(buff[0]);
b = buff[1];
}
}