Config Helper
WritePrivateProfileString Method
Example 



KellermanSoftware.ConfigHelper Namespace > IniReaderWriter Class : WritePrivateProfileString Method
The INI Section Name. Use GLOBAL for the global section.
The INI Setting Name
The INI Setting Value
The path to the INI file
Managed version of WritePrivateProfileString
No COM Interop is used
If the file does not exist it will be created. If the section does not exist it will be created. If the setting already exists it will be updated. If the setting does not exist, it will be added.
See: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724348%28v=vs.85%29.aspx
Syntax
Public Shared Function WritePrivateProfileString( _
   ByVal sectionName As String, _
   ByVal settingName As String, _
   ByVal settingValue As String, _
   ByVal filePath As String _
) As Boolean
Dim sectionName As String
Dim settingName As String
Dim settingValue As String
Dim filePath As String
Dim value As Boolean
 
value = IniReaderWriter.WritePrivateProfileString(sectionName, settingName, settingValue, filePath)
public static bool WritePrivateProfileString( 
   string sectionName,
   string settingName,
   string settingValue,
   string filePath
)
public:
static bool WritePrivateProfileString( 
   String^ sectionName,
   String^ settingName,
   String^ settingValue,
   String^ filePath
) 

Parameters

sectionName
The INI Section Name. Use GLOBAL for the global section.
settingName
The INI Setting Name
settingValue
The INI Setting Value
filePath
The path to the INI file

Return Value

True if the setting was set successfully
Exceptions
ExceptionDescription
Occurs when sectionName, settingName or filePath is null
Example
string expectedValue = "This is a test";
IniReaderWriter.WritePrivateProfileString("MySection", "MySettingName", expectedValue, "MySettingsFile.ini");
 
string actualValue;
 
int numberOfCharactersReturned = IniReaderWriter.GetPrivateProfileString("MySection", "MySettingName", string.Empty, out actualValue, 255, "MySettingsFile.ini");
Console.WriteLine(actualValue);
Dim expectedValue As String = "This is a test"
IniReaderWriter.WritePrivateProfileString("MySection", "MySettingName", expectedValue, "MySettingsFile.ini")
 
Dim actualValue As String
 
Dim numberOfCharactersReturned As Integer = IniReaderWriter.GetPrivateProfileString("MySection", "MySettingName", String.Empty, actualValue, 255, "MySettingsFile.ini")
Console.WriteLine(actualValue)
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

IniReaderWriter Class
IniReaderWriter Members