2014-09-18 4 views
1

저는 C#을 처음 사용하고 txt 파일의 내용을 기반으로 button.visible true/false를 구현하려고합니다. 현재까지 작성한 모든 내용은 기껏해야 불안정합니다. 이 대화 상자는 Winform 독립 실행 형 응용 프로그램의 기본 대화 상자입니다.로그인 비교를 기반으로 버튼 표시

이상적인 세계에서는 더 단순해야합니다. 코드를 Permissions.txt으로 열려면 MessageBox이 목록의 이름을 표시하고 Environment.UserName.txt의 모든 이름을 비교하면됩니다. 단추가 표시되면 새 대화 상자가 열립니다.

누구나 기꺼이 새로운 사람을 가르칩니다. 나는 잠시 동안을 찾고 있었고 나는 그것을 보지 못했다.

나는 또한 File.Readlines과 함께 작업을 시도했지만 성공하지 못했습니다.

제공 해 주신 지원에 대해 미리 감사드립니다.

프랭크 Pytel 편집을 할

public void hideWidget() 
    { 
     //gets the users login name from the system 
     string newName = userNameOnly(); 

     // Read the file and display it line by line. 
     System.IO.StreamReader file = 
      new System.IO.StreamReader(dataFolder + "\\Permissions.txt"); 


     //This next bit called Original Code works on my local when I access it, when accessed from a server, but not for other users. 
     //Original code 
     //while ((line = file.ReadLine()) != null) 
     //{ 
     // if (line == newName) 
     // { 
     //  WidgetForm.Visible = true; 
     // } 
     // else 
     // { 
     //  WidgetForm.Visible = false; 
     // } 
     // //MessageBox.Show(line); 
     // counter++; 
     //} 

     //file.Close();    


//This is where I am at currently. Again it's not picking up all of the names in the .txt file. 

     while (file.ReadLine() != null) 
     { 

      //string line; 
      string line = file.ReadLine(); 

      if (newName == file.ReadLine()) 
      { 
       WidgetForm.Visible = false; 
      } 
      else 
      { 
       WidgetForm.Visible = true; 
      } 
      int counter = 0; 

      //MessageBox.Show(line); 
      //MessageBox.Show(file.ReadLine()); 
      counter ++; 
     } 

     //file.Close(); 

    } 

.... 또한

가능하게 설명 할 수있는 사람이 있다면 어떻게 문자열 라인; 내 사용자 이름으로 설정됩니다. 그게 어떻게 설정되어 있어야하지만, 나는 그 라인 == newName 원래 코드에서 말한 적이 없어. 나는 그것이 While을위한 것이라고 생각했습니다. 그들이 동등한 지 확인하기 위해서 ..

최종 수정.

다음은 내가 작업해야하는 항목입니다. @Bedford에게 감사합니다.

이 부분은 직접 Form1 클래스

string[] lines = File.ReadAllLines(dataFolder + "\\Permissions.txt"); 

이 다른 사람들이 혜택을 누릴 수 있도록 나는이 조각을 게시있어 hideWidget() 버튼을

 public void hideWidget() 
    { 
     //Make all userNames available to the logic 
     string newName = userNameOnly(); 

     //variable to decide if userExists is true/false 
     bool userExists; 

     //Loop through all of the userNames in the file and see if it matches the userName login 
     while (lines != null) 
     { 
      //Decide to make the button available if userExists does exist in the file 
      if (lines != null) 
      { 
       userExists = lines.Any(ln => ln == newName); 

       WidgetForm.Visible = userExists; 
      } 

       //Do nothing if the userName does not match anyone in the Permissions.txt file. The button default Visible is false 
      else 
      { 

      } 
      return; 
     } 
    } 

뒤에 논리입니다 아래로 이동합니다. 다시 한번 감사드립니다. 이 NEWB는 도움을 정말 고맙게 생각합니다. 해가 !! :-)

당신은 File.ReadAllLines 정적 메서드를 사용하여 파일에서 모든 라인을 읽은 다음 라인의 사용자 이름과 일치 여부를 확인하기 위해 LINQ 쿼리를 사용할 수 있습니다

답변

2

: 항상

string[] lines = File.ReadAllLines(Path.Combine(dataFolder, "Permissions.txt")); 
bool userExists = lines.Any(ln => ln == newName); // or any comparison you like 

// use the bool variable to set the visibility 
WidgetForm.Visible = userExists; 
+0

을해야 'Path.Combine'을 사용하십시오. 내부적으로 인수가 백 슬래시로 시작/끝나는 지 알아 내고 제대로 결합합니다. –

+0

@ 머핀 맨 내가 한 것 ' 내부 정적 문자열 appPath = System.Windows.Forms.Application.StartupPath; 내부 정적 문자열 dataFolder = System.IO.Path.Combine (appPath, "Data"); 내부 정적 문자열 reportFolder = System.IO.Path.Combine (appPath, "Report"); 내부 정적 문자열 weeklyFolder = System.IO.Path.Combine (appPath, "Weekly"); ' –

+0

새로운 소식입니다. 나는 그 내부가 올바르게 결합을 했길 바래 ?? –

관련 문제