Text and Ini FileIO with Silk4Net
Silk4Net contains no native methods for reading and writing to text or ini files. Attached to this resolution is the vb.Net source code and the compiled dll for a fileIO class library that will allow you to read and write to text files and ini files. These are the equivalent of the SilkTest Classic Filexxxx and IniFilexxxx methods. In the case of the Filexxxx methods they have been compressed to two methods only - readLine and writeLine - as file open and file close is done automatically when the read and write methods are called.
The inifile methods are a wrapper around the Read/Write PrivateProfile functions supplied with pre- dotNet versions of Visual Studio. Microsoft would prefer users of Visual Studio to move to storing information directly in the registry or as an xml file but there are many tens of thousands of applications that still use the much simpler inifile structure.
Although the examples below show hard-coded strings you can just as easily use variables to simplify your code.
You will need to add a reference to the fileIO.dll in your Silk4Net project to enable usage of the class libraries. The white paper 'Creating Tags for Silk4Net' (article 31618) covers this process in detail. The fileIO.dll itself is in the \bin\debug folder; to view the source code for the project unzip the archive to a new folder and select the top-level fileIO.sln file.
The constructors and method calls are broken down in detail below.
Text file commands
fileIO.fileIO = new fileIO.fileIO();
fileIO.fileIO fileReadWrite = new fileIO.fileIO();
<[optional] boolean>=writeLine(filename, data, [optional] writetype)
where writetype = 'append' or 'new'.
The default write method is 'append' but if the file does not exist it will be created. Similarly, if 'new' is called and the file exists then the original file will be overwritten. writeLine can return an optional 'true' or 'false' to indicate success or failure.
e.g. to append a line without checking if the action has succeeded or failed and without knowing if the file currently exists:
fileReadWrite.writeLine("c:\\temp\\mydata.txt","new data line")
readLine(filename, [optional] startpoint)
where startpoint =
-1 to start again from the top of the file
0 to continue with the next line
any integer to read a specific line.
The startpoint default is zero.
readLine will return 'eof' as data for end of file or for an invalid line number. It will also return 'file not found' as data if it fails to open or locate the specified file.
e.g. to force a read to start again from the top of the file:
data = fileReadWrite.readLine("c:\\temp\\mydata.txt",-1);
INI file commands
The ini file methods stick rigidly to the old Read/Write PrivateProfile functions pattern of sectionname, keyname, default value and ini file name.
fileIO.Ini = new fileIO.Ini()
fileIO.Ini myIniFile = new fileIO.Ini()
WriteIniString(sectionname, keyname, string, inifilename)
WriteIniInteger(sectionname, keyname, integer, inifilename)
e.g. to write string data to the ini file
myIniFile.WriteIniString("testsection", "string1", "micro focus", "c:\\temp\\initest.ini");
ReadIniString(sectionname, keyname, default value as string, inifilename)
ReadIniInteger(sectionname, keyname, default value as integer, inifilename)
e.g. to read an integer with a default return value of -1
myIniFile.ReadIniInteger("testsection", "integer1", -1, "c:\\temp\\initest.ini");