you can use "
Environment.SpecialFolder.LocalApplicationData
" for such things:
string path = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),this.AssemblyCompany), this.AssemblyProduct);
A very basic usage:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test_Path1
{
class Program
{
public static string AssemblyCompany = "Company1";
public static string AssemblyProduct = "App1";
static void Main(string[] args)
{
string connStr = @"Data Source=.\sqlexpress; initial catalog=TEST; integrated security=true;";
string myweb = @"http://google.com";
string allConfig = connStr + Environment.NewLine + myweb;
MyConfig cfg = new MyConfig();
cfg.Save(allConfig);
string[] rows = cfg.Read();
Console.WriteLine(rows[0]);
Console.WriteLine(rows[1]);
}
}
class MyConfig
{
public string ConfigPath
{
get
{
return Path.Combine(Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
Program.AssemblyCompany), Program.AssemblyProduct);
}
}
public MyConfig()
{
if (!Directory.Exists(this.ConfigPath))
Directory.CreateDirectory(this.ConfigPath);
}
public void Save(string config)
{
using (StreamWriter s = new StreamWriter(Path.Combine(this.ConfigPath, "MyConfig.txt")))
{
s.Write(config);
}
}
public string[] Read()
{
using (StreamReader s = new StreamReader(Path.Combine(this.ConfigPath, "MyConfig.txt")))
{
return s.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
}
}
}
}