Files
CMS-MTConn/MTC_Sim/SCMCncLib/IniFile.cs
T
2016-05-25 16:51:57 +02:00

395 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Globalization;
//using System.Windows.Forms;
//using System.Windows.Interop;
namespace IniFiles
{
/// <summary>
/// Create a new INI file to store or load data
/// </summary>
public class IniFile
{
public string FileName; // INI filename
// import windows dll functions
#region DLL_IMPORT_FUNCTIONS
[DllImport("kernel32", CharSet = CharSet.Auto, BestFitMapping=false)]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern bool WritePrivateProfileSection(string section, string value, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileInt(string section, string key, int def, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileSection(string section, IntPtr retVal, uint size, string filePath);
#endregion
/// <summary>
/// Constructor
/// </summary>
/// <PARAM name="INIPath"></PARAM>
public IniFile(string INIPath)
{
FileName = INIPath;
}
/// <summary>
/// Write data to the INI file
/// </summary>
/// <PARAM name="Section"></PARAM>
/// Section name
/// <PARAM name="Key"></PARAM>
/// Key Name
/// <PARAM name="Value"></PARAM>
/// Value Name
public void WriteString(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, FileName);
}
/// <summary>
/// Write a boolean value
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="Value"></param>
public void WriteBoolean(string Section, string Key, bool Value)
{
int flag = Value ? 1 : 0;
WriteString(Section, Key, Convert.ToString(flag));
}
/// <summary>
/// Write an integer value
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="Value"></param>
public void WriteInteger(string Section, string Key, int Value)
{
WriteString(Section, Key, Convert.ToString(Value));
}
/// <summary>
/// Write a double value
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="Value"></param>
public void WriteDouble(string Section, string Key, double Value)
{
WriteString(Section, Key, Convert.ToString(Value, NumberFormatInfo.InvariantInfo));
}
/// <summary>
/// Read data from the Ini file
/// </summary>
/// <PARAM name="Section"></PARAM>
/// <PARAM name="Key"></PARAM>
/// <PARAM name="Path"></PARAM>
/// <returns></returns>
public string ReadString(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, FileName);
return temp.ToString();
}
/// <summary>
/// Read a string. If not found use default value
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="DefaultVal"></param>
/// <returns></returns>
public string ReadString(string Section, string Key, string DefaultVal)
{
string temp = ReadString(Section, Key);
if (temp == "") temp = DefaultVal;
return temp;
}
/// <summary>
/// Read an integer
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <returns></returns>
public int ReadInteger(string Section, string Key)
{
return GetPrivateProfileInt(Section, Key, 0, FileName);
//System.Convert.ToInt32(IniReadValue(Section, Key));
}
/// <summary>
/// Read an integer. If not found use default value
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="DefaultVal"></param>
/// <returns></returns>
public int ReadInteger(string Section, string Key, int DefaultVal)
{
//int temp = System.Convert.ToInt32(IniReadString(Section, Key, Convert.ToString(DefaultVal)));
//return temp;
return GetPrivateProfileInt(Section, Key, DefaultVal, FileName);
}
/// <summary>
/// Read a boolean
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <returns></returns>
public bool ReadBoolean(string Section, string Key)
{
return (ReadInteger(Section, Key, 0) != 0);
}
/// <summary>
/// Read a boolean with default value
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="DefaultVal"></param>
/// <returns></returns>
public bool ReadBoolean(string Section, string Key, bool DefaultVal)
{
int v = DefaultVal ? 1 : 0;
return (ReadInteger(Section, Key, v) != 0);
}
/// <summary>
/// Read a complete section (keys=values)
/// </summary>
/// <param name="Section"></param>
/// Section name
/// <returns></returns>
public string[] ReadSection(string Section)
{
const int bufferSize = 2048; // max is 32767
StringBuilder returnedString = new StringBuilder();
IntPtr pReturnedString = Marshal.AllocCoTaskMem(bufferSize);
try
{
int bytesReturned = GetPrivateProfileSection(Section, pReturnedString, bufferSize, FileName);
//bytesReturned -1 to remove trailing \0
for (int i = 0; i < bytesReturned - 1; i++)
returnedString.Append((char)Marshal.ReadByte(new IntPtr((uint)pReturnedString + (uint)i)));
}
finally
{
Marshal.FreeCoTaskMem(pReturnedString);
}
string sectionData = returnedString.ToString();
return sectionData.Split('\0');
}
/// <summary>
/// Return true if value exists
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <returns></returns>
public bool ValueExists(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, FileName);
return (i > 0);
}
#if false
/// <summary>
/// Write a form position and size
/// </summary>
/// <param name="Name"></param>
/// <param name="Form"></param>
/// <param name="PositionOnly">If true store position only (tools and dialogs windows)</param>
public void WriteWindowPosition(String Name, Window Form, bool PositionOnly)
{
if (Form.WindowState == WindowState.Normal)
{
WriteDouble("SIZE", Name + "_Top", Form.Top);
WriteDouble("SIZE", Name + "_Left", Form.Left);
if (!PositionOnly)
{
WriteDouble("SIZE", Name + "_Height", Form.Height);
WriteDouble("SIZE", Name + "_Width", Form.Width);
}
}
WriteBoolean("SIZE", Name + "_Maximized", Form.WindowState == WindowState.Maximized);
Screen screen = Screen.FromHandle(new WindowInteropHelper(Form).Handle);
WriteDouble("SIZE", Name + "_ScreenLeft", screen.WorkingArea.Left);
WriteDouble("SIZE", Name + "_ScreenTop", screen.WorkingArea.Top);
}
/// <summary>
/// Write a form position
/// </summary>
/// <param name="Name"></param>
/// <param name="Form"></param>
public void WriteWindowPosition(String Name, Window Form)
{
WriteWindowPosition(Name, Form, false);
}
/// <summary>
/// Read a form position and size
/// </summary>
/// <param name="Name"></param>
/// <param name="Form"></param>
/// <param name="PositionOnly"></param>
public void ReadWindowPosition(String Name, Window Form, bool PositionOnly)
{
if (ValueExists("SIZE", Name + "_Top"))
{
// limit to resolution
double maxW = SystemParameters.VirtualScreenWidth;
double maxH = SystemParameters.VirtualScreenHeight;
Form.WindowStartupLocation = WindowStartupLocation.Manual;
Form.Top = ReadInteger("SIZE", Name + "_Top", 0);
Form.Left = ReadInteger("SIZE", Name + "_Left", 0);
if (!PositionOnly)
{
Form.Height = ReadInteger("SIZE", Name + "_Height", 0);
Form.Width = ReadInteger("SIZE", Name + "_Width", 0);
if (Form.Width > maxW)
{
Form.Width = maxW;
}
if (Form.Height > maxH)
{
Form.Height = maxH;
}
}
// limit position
if ((Form.Left + Form.Width) > maxW)
{
Form.Left = maxW - Form.Width;
}
if ((Form.Top + Form.Height) > maxH)
{
Form.Top = maxH - Form.Height;
}
}
else
Form.WindowStartupLocation = WindowStartupLocation.CenterScreen;
// when maximized, restore to screen position also
if (ReadBoolean("SIZE", Name + "_Maximized", false))
{
Form.WindowStartupLocation = WindowStartupLocation.Manual;
Form.Left = ReadInteger("SIZE", Name + "_ScreenLeft", 0);
Form.Top = ReadInteger("SIZE", Name + "_ScreenTop", 0);
// until form is loaded it's not possible to maximize to a different screen
if (Form.IsLoaded || (Form.Left == 0))
{
Form.WindowState = WindowState.Maximized;
}
else
{
// set Tag to maximize in Loaded event
Form.Tag = 1;
}
}
}
/// <summary>
/// Used to restore position when restoring (maximized -> normal)
/// </summary>
/// <param name="Name"></param>
/// <param name="Form"></param>
public void ReadRestoredWindowPosition(String Name, Window Form)
{
if (ValueExists("SIZE", Name + "_Top"))
{
// limit to resolution
double maxW = SystemParameters.VirtualScreenWidth;
double maxH = SystemParameters.VirtualScreenHeight;
Form.WindowStartupLocation = WindowStartupLocation.Manual;
Form.Top = ReadInteger("SIZE", Name + "_Top", 0);
Form.Left = ReadInteger("SIZE", Name + "_Left", 0);
// limit position
if ((Form.Left + Form.Width) > maxW)
{
Form.Left = maxW - Form.Width;
}
if ((Form.Top + Form.Height) > maxH)
{
Form.Top = maxH - Form.Height;
}
}
else
Form.WindowStartupLocation = WindowStartupLocation.CenterScreen;
}
/// <summary>
/// Read a form position
/// </summary>
/// <param name="Name"></param>
/// <param name="Form"></param>
public void ReadWindowPosition(String Name, Window Form)
{
ReadWindowPosition(Name, Form, false);
}
#endif // false
/// <summary>
/// Completely remove one section
/// </summary>
/// <param name="Section"></param>
public void IniDeleteSection(string Section)
{
WritePrivateProfileSection(Section, null, FileName);
}
/// <summary>
/// Return true if section exists
/// </summary>
/// <param name="Section"></param>
/// <returns></returns>
public bool IniSectionExists(string Section)
{
int bytesReturned = 0;
const int bufferSize = 2048; // max is 32767
IntPtr pReturnedString = Marshal.AllocCoTaskMem(bufferSize);
try
{
bytesReturned = GetPrivateProfileSection(Section, pReturnedString, bufferSize, FileName);
}
finally
{
Marshal.FreeCoTaskMem(pReturnedString);
}
return (bytesReturned > 0);
}
/// <summary>
/// Delete a key from section
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
public void IniDeleteKey(string Section, string Key)
{
WritePrivateProfileString(Section, Key, null, FileName);
}
}
}