File problems

  • Thread starter Thread starter Shane Mergy
  • Start date Start date
S

Shane Mergy

i have a program that reads through an ini file and does stuff so far
the only thing i am having trouble with is the writing to the ini
file. if the section is alreadey there i want to do a search for that
line then all text to the next line of end of that block can someone
help with this.
 
Shane,

Are you doing this parsing yourself? Have you considered using the
WritePrivateProfileString/WritePrivateProfileSection API functions? You can
call these to write your INI file sections, and you can use the
ReadPrivateProfileString/ReadPrivateProfileSection API functions to read the
same INI file.

Hope this helps.
 
You should better use a class specially for settings. This class must be
Serializable

This is an exapmle that saves and retrievs the setting for the font size of
a textBox in a file

-----------Settings Class---------------
using System;

[Serializable]

public class settingsClass

{

public settingsClass(float size,int delay)


{

fontSize=size;

programDelay=delay;

}

#region Font Size

private float fontSize;

public float FontSize

{

get

{

return fontSize;

}

set

{

fontSize=value;

}

}

#endregion Font Size

}




-----------loadSaveSettingsClass--------------------------------
using System.IO;
using System.Drawing;
using System.Runtime.Serialization.Formatters.Binary;

public class loadSaveSettingsClass
{
public loadSaveSettingsClass(main mf)
{
mainForm=mf;
}

private static main mainForm;

private static BinaryFormatter binaryFormatter = new BinaryFormatter();

private static FileStream fileStream;
private static settingsClass settings;

#endregion ???????? ??????????


public static void loadSettings()
{
settings=new settingsClass(0,0);

string fileName=(string)mainForm.ProgramDirectory+"\\settings.dat";

try
{
fileStream=new FileStream(fileName,FileMode.Open,FileAccess.Read);
settings=(settingsClass) binaryFormatter.Deserialize(fileStream);
}

catch (System.IO.FileNotFoundException)
{
return;
}

catch (System.Runtime.Serialization.SerializationException)
{
fileStream.Close();
return;
}

fileStream.Close();

mainForm.sourceCode.Font=new
Font(mainForm.sourceCode.Font.FontFamily,settings.FontSize);
}

public static void saveSettings()
{
settings = new
settingsClass(mainForm.sourceCode.Font.Size,mainForm.delayTrackBar.Value);

string fileName=(string)mainForm.ProgramDirectory+"\\settings.dat";

fileStream=new
FileStream(fileName,FileMode.OpenOrCreate,FileAccess.Write);

binaryFormatter.Serialize(fileStream,settings);

fileStream.Close();
}
#endregion Save Setting
}
 
Shane,

Here is the declaration for WritePrivateProfileString:

[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool WritePrivateProfileString(
[MarshalAs(UnmanagedType.LPTStr)] string lpAppName,
[MarshalAs(UnmanagedType.LPTStr)] string lpKeyName,
[MarshalAs(UnmanagedType.LPTStr)] string lpString,
[MarshalAs(UnmanagedType.LPTStr)] string lpFileName);

And here is the declaration for WritePrivateProfileSection:

[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool WritePrivateProfileSection(
[MarshalAs(UnmanagedType.LPTStr)] string lpAppName,
[MarshalAs(UnmanagedType.LPTStr)] string lpString,
[MarshalAs(UnmanagedType.LPTStr)] string lpFileName);

You will have to have the "using System.Runtime.InteropServices;"
directive at the top of the file as well.

Hope this helps.
 
Back
Top