操作INI文件以及开机自动运行
public class VolumnCfg
{
public static string iniFile;
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
/// <summary>
/// 构造方法
/// </summary>
/// <param name="iniFile">文件路径</param>
public VolumnCfg()
{
iniFile = System.Windows.Forms.Application.StartupPath + "\\VolumnCfg.ini";
if (!File.Exists(iniFile))
initCfgFile(iniFile);
}
public static void initCfgFile(string FilePath)
{
IniWriteValue("VolumnCfg", "AutoRun", System.Boolean.FalseString);
IniWriteValue("VolumnCfg", "Opacity", "0.8");
IniWriteValue("VolumnCfg", "DelaySec", "3");
}
/// <summary>
/// 写入INI文件
/// </summary>
/// <param name="Section">项目名称(如 [DataTP] )</param>
/// <param name="Key">键</param>
/// <param name="Value">值</param>
public static void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, iniFile);
}
/// <summary>
/// 读出INI文件
/// </summary>
/// <param name="Section">项目名称(如 [DataTP] )</param>
/// <param name="Key">键</param>
public static string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(500);
int i = GetPrivateProfileString(Section, Key, "", temp, 500, iniFile);
return temp.ToString();
}
/// <summary>
/// 开机启动项
/// </summary>
/// <param name="Started">是否启动</param>
/// <param name="name">启动值的名称</param>
/// <param name="path">启动程序的路径</param>
public static void SetAutoRun(bool Started, string name, string path)
{
RegistryKey HKLM = Registry.LocalMachine;
RegistryKey Run = HKLM.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
if (Started == true)
{
try
{
Run.SetValue(name, path);
HKLM.Close();
}
catch (Exception)
{
//MessageBox.Show(Err.Message.ToString(), "MUS", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
转载:http://www.cnblogs.com/zhahost/archive/2008/06/30/1232534.html
{
public static string iniFile;
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
/// <summary>
/// 构造方法
/// </summary>
/// <param name="iniFile">文件路径</param>
public VolumnCfg()
{
iniFile = System.Windows.Forms.Application.StartupPath + "\\VolumnCfg.ini";
if (!File.Exists(iniFile))
initCfgFile(iniFile);
}
public static void initCfgFile(string FilePath)
{
IniWriteValue("VolumnCfg", "AutoRun", System.Boolean.FalseString);
IniWriteValue("VolumnCfg", "Opacity", "0.8");
IniWriteValue("VolumnCfg", "DelaySec", "3");
}
/// <summary>
/// 写入INI文件
/// </summary>
/// <param name="Section">项目名称(如 [DataTP] )</param>
/// <param name="Key">键</param>
/// <param name="Value">值</param>
public static void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, iniFile);
}
/// <summary>
/// 读出INI文件
/// </summary>
/// <param name="Section">项目名称(如 [DataTP] )</param>
/// <param name="Key">键</param>
public static string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(500);
int i = GetPrivateProfileString(Section, Key, "", temp, 500, iniFile);
return temp.ToString();
}
/// <summary>
/// 开机启动项
/// </summary>
/// <param name="Started">是否启动</param>
/// <param name="name">启动值的名称</param>
/// <param name="path">启动程序的路径</param>
public static void SetAutoRun(bool Started, string name, string path)
{
RegistryKey HKLM = Registry.LocalMachine;
RegistryKey Run = HKLM.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
if (Started == true)
{
try
{
Run.SetValue(name, path);
HKLM.Close();
}
catch (Exception)
{
//MessageBox.Show(Err.Message.ToString(), "MUS", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
转载:http://www.cnblogs.com/zhahost/archive/2008/06/30/1232534.html