473,320 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

File problems

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.
Nov 15 '05 #1
4 3094
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.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Shane Mergy" <mo***@ameritech.net> wrote in message
news:41**************************@posting.google.c om...
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.

Nov 15 '05 #2
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+"\\sett ings.dat";

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

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

catch (System.Runtime.Serialization.SerializationExcepti on)
{
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,mainFo rm.delayTrackBar.Value);

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

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

binaryFormatter.Serialize(fileStream,settings);

fileStream.Close();
}
#endregion Save Setting
}
"Shane Mergy" <mo***@ameritech.net> wrote in message
news:41**************************@posting.google.c om...
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.

Nov 15 '05 #3


how would i do this
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #4
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.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Shane Mergy" <mo***@ameritech.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...


how would i do this
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: HNguyen | last post by:
Hi, I have a Web application in ASP.NET. My Application allows the users upload files into the server after checking their user names and passwords. For each transaction, the Web program will...
12
by: Brian Henry | last post by:
first question... I have a flat file which unfortinuatly has columns seperated by nulls instead of spaces (a higher up company created it this way for us) is there anyway to do a readline with this...
8
by: Sarah | last post by:
I need to access some data on a server. I can access it directly using UNC (i.e. \\ComputerName\ShareName\Path\FileName) or using a mapped network drive resource (S:\Path\FileName). Here is my...
5
by: Claudio Grondi | last post by:
I have just started to play around with the bsddb3 module interfacing the Berkeley Database. Beside the intended database file databaseFile.bdb I see in same directory also the __db.001...
7
by: ashley.ward | last post by:
We have been using VB6 to develop small custom apps that access an Oracle database, in order to extend a larger product that is developed by our colleagues in Germany (who use C++ and Java). As...
2
by: Jack Fox | last post by:
We are encountering a couple of problems with our ASP.NET / IIS 6.0 applications: In each of 3 production environments we maintain a Windows Server 2003 machine running NTFS as a file server....
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
29
by: list | last post by:
Hi folks, I am new to Googlegroups. I asked my questions at other forums, since now. I have an important question: I have to check files if they are binary(.bmp, .avi, .jpg) or text(.txt,...
34
by: Alexnb | last post by:
Gerhard Häring wrote: No, it didn't work, but it gave me some interesting feedback when I ran it in the shell. Heres what it told me: Traceback (most recent call last): File...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.