Reputation: 71
This should be an easy one...for someone else. I'm reading a text file with some values in it, and breaking those values down into individual values...blah blah. Basically I need to know how to retrieve the value from the second line in my text file. My code already reads the first value from the first line. How do I repeat this process as I will have 20-30 different lines with different values on them?
namespace oSnaps
{
public partial class Form1 : Form
{
// read the settings file and disect the value of OSMODE
private String settingsPath = "N:\\C3D Support\\MySettings.txt";
private enum oSnap : int
{
none = 0, // =0
endpoint = 1 << 0, // =1
midpoint = 1 << 1, // =2
center = 1 << 2, // =4
node = 1 << 3, // =8
quadrant = 1 << 4, // =16
intersection = 1 << 5, // =32
insertion = 1 << 6, // =64
perpendicular = 1 << 7, // =128
tangent = 1 << 8, // =256
nearest = 1 << 9, // =512
apparentIntersection = 1 << 11, // =2048
extension = 1 << 12, // =4096
parallel = 1 << 13, // =8192
defaultmode = 1 << 0, // =1
editmode = 1 << 1, // =2
commandactive = 1 << 2, // =4
commandmode = 1 << 3, // =8
menumode = 1 << 4, // =16
}
public Form1()
{
InitializeComponent();
LoadSettings();
}
private void LoadSettings()
{
if (!System.IO.File.Exists(settingsPath))
{
try
{
StreamWriter file = new StreamWriter(settingsPath);
file.WriteLine("OSNAPS,0" + Environment.NewLine + "Mouse Value,0");
file.Close();
}
catch
{
MessageBox.Show("MySettings.txt was unaccessable. Contact the IT Department if you see this message.");
return;
}
}
string[] lines = File.ReadAllLines(settingsPath);
SetOsnaps(lines);
}
private void SetOsnaps(string[] lines)
{
try
{
// First line = lines[0]
int val = Convert.ToInt32(lines[0].Split(',')[1]);
if ((val & 1) == 1) { cbxEndpoint.Checked = true; }
if ((val & 2) == 2) { cbxMidpoint.Checked = true; }
if ((val & 4) == 4) { cbxCenter.Checked = true; }
if ((val & 8) == 8) { cbxNode.Checked = true; }
if ((val & 16) == 16) { cbxQuadrant.Checked = true; }
if ((val & 32) == 32) { cbxIntersection.Checked = true; }
if ((val & 64) == 64) { cbxInsertion.Checked = true; }
if ((val & 128) == 128) { cbxPerpendicular.Checked = true; }
if ((val & 256) == 256) { cbxTangent.Checked = true; }
if ((val & 512) == 512) { cbxNearest.Checked = true; }
if ((val & 2048) == 2048) { cbxApparent.Checked = true; }
if ((val & 4096) == 4096) { cbxExtension.Checked = true; }
if ((val & 8192) == 8192) { cbxParallel.Checked = true; }
// Second line = lines[1]
int mval = Convert.ToInt32(lines[1].Split(',')[1]);
if ((val & 1) == 1) { cbxRcDefault.Checked = true; }
if ((val & 2) == 2) { cbxRcEdit.Checked = true; }
if ((val & 4) == 4) { cbxRcCommandActive.Checked = true; }
if ((val & 8) == 8) { cbxRcCommand.Checked = true; }
if ((val & 16) == 16) { cbxRcMenu.Checked = true; }
}
Text file example:
val, 768
mval, 12
Upvotes: 0
Views: 673
Reputation: 216348
Given the context (a short file with only two lines) then you could simply read all lines from your file, then pass all of them to your function
private void LoadSettings()
{
if (!System.IO.File.Exists(settingsPath))
{
try
{
System.IO.File.WriteAllText(settingsPath, "OSNAPS,0" + Environment.NewLine + "Mouse Value,0");
}
catch
{
MessageBox.Show("MySettings.txt was unaccessable. Contact the IT Department if you see this message.");
return;
}
}
string[] lines = System.IO.File.ReadAllLines(settingsPath);
SetOsnaps(lines);
}
private void SetOsnaps(string[] lines)
{
try
{
// First line = lines[0]
int val = Convert.ToInt32(lines[0].Split(',')[1]);
if ((val & 1) == 1) { cbxEndpoint.Checked = true; }
if ((val & 2) == 2) { cbxMidpoint.Checked = true; }
if ((val & 4) == 4) { cbxCenter.Checked = true; }
if ((val & 8) == 8) { cbxNode.Checked = true; }
if ((val & 16) == 16) { cbxQuadrant.Checked = true; }
if ((val & 32) == 32) { cbxIntersection.Checked = true; }
if ((val & 64) == 64) { cbxInsertion.Checked = true; }
if ((val & 128) == 128) { cbxPerpendicular.Checked = true; }
if ((val & 256) == 256) { cbxTangent.Checked = true; }
if ((val & 512) == 512) { cbxNearest.Checked = true; }
if ((val & 2048) == 2048) { cbxApparent.Checked = true; }
if ((val & 4096) == 4096) { cbxExtension.Checked = true; }
if ((val & 8192) == 8192) { cbxParallel.Checked = true; }
// Second line = lines[1]
int mval = Convert.ToInt32(lines[1].Split(',')[1]);
if ((val & 1) == 1) { cbxRcDefault.Checked = true; }
if ((val & 2) == 2) { cbxRcEdit.Checked = true; }
if ((val & 4) == 4) { cbxRcCommandActive.Checked = true; }
if ((val & 8) == 8) { cbxRcCommand.Checked = true; }
if ((val & 16) == 16) { cbxRcMenu.Checked = true; }
}
Upvotes: 1
Reputation: 1700
have you tried to read about reading from files? is seems not :/
bool isFirst = true;
using (var reader = new StreamReader(filePath))
{
while(!reader.EndOfStream)
{
if (isFirst)
{
isFirst = false;
continue;
}
SetOsnaps(reader.ReadLine());
}
}
Upvotes: 0