using System;
using System.Configuration;
using System.IO;

namespace DatabaseEditor
{
    public static class ConnectionStringReader
    {
        /// <summary>
        /// Reads connection string from file.
        /// </summary>
        /// <param name="path">Path to the file. If not specified, default from App.config is taken</param>
        /// <returns>Connection string</returns>
        public static string GetConnectionString(string path = null)
        {
            if (path == null)
            {
                try
                {
                    path = ConfigurationManager.AppSettings["ConnectionStringPath"];
                    path = Path.GetFullPath(path);
                }
                catch (Exception e)
                {
                    throw new Exception("Failed to read connection string.", e);
                }
            }
            if (File.Exists(path))
            {
                return File.ReadAllText(path);
            }
            return null;
        }
    }
}