2014-03-05 85 views
0

對於其他人來說,這應該是一件容易的事。我正在閱讀一個帶有一些值的文本文件,並將這些值分解爲單個值......等等。基本上我需要知道如何從我的文本文件的第二行檢索值。我的代碼已經從第一行讀取第一個值。我如何重複這個過程,因爲我將有20-30條不同的線,其上有不同的值?從文本文件的第二行讀取

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; } 

      } 

文本文件,例如:

VAL,768

MVAL,12

+1

不你的'line'參數只包含你文本文件的第一行,或者整個文件的內容? –

+0

我們根本不需要看到'SetOsnaps'。我們需要看到正在讀取文件中的行並調用SetOsnaps的代碼。我們的想法是將正確的數據傳遞給'SetOsnaps',而不是修改'SetOsnaps'。 – Servy

+0

編輯我的代碼示例 – smakfactor1

回答

1

考慮上下文(只有兩行簡短的文件),那麼你可以簡單地從文件中讀取所有行,然後通過他們都你的函數

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; } 

} 
+0

謝謝。儘管如此,仍然存在一個問題。它似乎還沒有讀第二行。它只是在兩種情況下都使用第一個實例。即如果val實際= 24它讀取mval爲24也 – smakfactor1

+0

你確定該文件包含兩行之間沒有空行嗎?你檢查了第二個索引器('lines [1]')嗎?請在調用SetOsnaps方法之前添加一個MessageBox(行[0])和另一個MessageBox(行[1]) – Steve

+0

已更新我的原始帖子。是的,文本文件中目前有3行。每行都是一個逗號,然後至少爲0.嗯...讀回我的代碼,它看起來像我需要另一個枚舉器...... – smakfactor1

0

你試圖瞭解從文件中讀取?是好像沒有:/

bool isFirst = true; 

using (var reader = new StreamReader(filePath)) 
{ 
    while(!reader.EndOfStream) 
    { 
     if (isFirst) 
     { 
      isFirst = false; 
      continue; 
     } 

     SetOsnaps(reader.ReadLine()); 
    } 
}