2014-03-05 3 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; } 

      } 

텍스트 파일 예를

:

발, 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 (lines [0])와 다른 하나의 MessageBox (lines [1])를 추가하십시오. – Steve

+0

원본 게시물을 업데이트했습니다. 네, 텍스트 파일에 현재 세 줄이 있습니다. 각 줄의 단어는 쉼표 다음에 최소 0의 값입니다. 흠 ... 내 코드를 다시 읽으면 다른 열거자를 필요로하는 것처럼 보입니다 ... – smakfactor1

0

당신은 파일에서 읽기에 대해 읽고 시도? 없는 것 같다입니다 : 컨텍스트 (두 줄 짧은 파일)을 감안할 때/

bool isFirst = true; 

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

     SetOsnaps(reader.ReadLine()); 
    } 
} 
관련 문제