Reading string from .txt file and finding values you need  
Author Message
UsingBytes





PostPosted: Visual C# Language, Reading string from .txt file and finding values you need Top

Hello all,

I havent worked with string in awhile but now in my application I need to set some values in a .txt file that will be read once the application is installed (be able to tell if application is on first startup etc...)

Anyway the class I have created to do this is all working, and I'm getting the values I need. I'm just wondering if there is a better way to do this

Here is the content of my .txt file, just 1 line right now.

FIRSTRUN-TRUE;

And here is the code for the class. But basically i want to read in the data, throw it into an array, then read the array value/value's, parse out to only get the information i need. In the above line I only need "TRUE"

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;

using CGDataManager.Utility;

namespace CGDataManager.Utility
{
public class GetApplicationSettings
{
private string[] fileArray;
public enum AppFileLayout
{
FIRSTSTARTUP = 0,
NUMBEROFELEMENTS = 1
}

public GetApplicationSettings()
{
fileArray =
new string[(int)AppFileLayout.NUMBEROFELEMENTS];
string filePath = FileManager.GetMediaFile("ApplicationSettings.txt");

try
{
if (File.Exists(filePath))
{
using (StreamReader sr = new StreamReader(filePath))
{
string line;
int position = (int)AppFileLayout.FIRSTSTARTUP;

while ((line = sr.ReadLine()) != null)
{
fileArray.SetValue(line, position);
position++;
}
}
}
}

catch (Exception e)
{
System.Windows.Forms.
MessageBox.Show(e.ToString(), "Error reading file!");
}
}

public bool GetFirstStartup()
{
bool value = true;
string startupValue = fileArray.GetValue((int)AppFileLayout.FIRSTSTARTUP).ToString();
int start = startupValue.IndexOf("-");
int end = startupValue.IndexOf(";");

startupValue = startupValue.Substring(start + 1, end - start - 1);

if (startupValue == "TRUE")
value =
true;

if (startupValue == "FALSE")
value =
false;

return value;
}
}
}

Anyway again, if there is a quicker way it would be great to know. Thanks again for any replies.



Visual C#10  
 
 
Paul Louth





PostPosted: Visual C# Language, Reading string from .txt file and finding values you need Top

Double click on Properties in your Solution Explorer.  There's a fully fledged property manager built in.  Just enter FirstRun = true, then access it via Settings.Default.FirstRun.  Once you have loaded it you can set it to false and then execute Settings.Save();

 


 
 
UsingBytes





PostPosted: Visual C# Language, Reading string from .txt file and finding values you need Top

ahh, thanks paul!

Just checked out the msdn on it, and thats exactly what I need.

Thanks again.


 
 
Paul Louth





PostPosted: Visual C# Language, Reading string from .txt file and finding values you need Top

Excellent!  Glad to help.  :)