막간을 이용해 C# 개발을 진행중입니다. 그동안 INI로 설정파일을 만들다가 INI의 고질적인 한 줄에 255자 밖에 지원하지 못하는 단점이 있기에 이번에 XML로 옮겨타기 위해 만들었습니다. 함수 전체적인 방식은 파일이 폴더내에 존재하면 파일에서 내용을 가지고 오고 없으면 초기값을 설정하고 초기값을 전역변수에 설정합니다.
CSHARP
XmlDocument xmlDoc = new XmlDocument(); g_SetFile = Class.EMCommon.GetCurPath() + @"\Config.xml"; if (Class.EMCommon.IsFileExist(g_SetFile) == true) { xmlDoc.Load(g_SetFile); XmlNodeList nl = xmlDoc.GetElementsByTagName("config"); g_MDBPath = nl[0]["MDBPath"].InnerText; g_DataPath = nl[0]["DataPath"].InnerText; g_LogPath = nl[0]["LogPath"].InnerText; g_InputPath = nl[0]["InputPath"].InnerText; } else { xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes")); XmlNode root = xmlDoc.CreateElement("", "config", ""); xmlDoc.AppendChild(root); XmlNode mdbPath = xmlDoc.CreateElement("", "MDBPath", ""); mdbPath.InnerText = Class.EMCommon.GetCurPath() + @"\SmartDEMIS-DER.mdb"; root.AppendChild(mdbPath); XmlNode dataPath = xmlDoc.CreateElement("", "DataPath", ""); dataPath.InnerText = Class.EMCommon.GetCurPath() + @"\Data"; root.AppendChild(dataPath); XmlNode logPath = xmlDoc.CreateElement("", "LogPath", ""); logPath.InnerText = Class.EMCommon.GetCurPath() + @"\Log"; root.AppendChild(logPath); XmlNode inputPath = xmlDoc.CreateElement("", "InputPath", ""); inputPath.InnerText = Class.EMCommon.GetCurPath() + @"\Input"; root.AppendChild(inputPath); xmlDoc.Save(g_SetFile); g_MDBPath = mdbPath.InnerText; g_DataPath = dataPath.InnerText; g_LogPath = logPath.InnerText; g_InputPath = inputPath.InnerText; }
g_SetFile은 전역변수입니다.
Class.EMCommon.IsFileExist 함수는 파일존재 유무를 알기 위한 함수입니다. File.Exist를 사용했습니다.