C# 根据登录比较显示按钮

C# 根据登录比较显示按钮,c#,winforms,c#-4.0,C#,Winforms,C# 4.0,我是C#新手,我正在尝试基于txt文件的内容实现一个button.visible true/false。到目前为止,我所写的一切充其量都是不稳定的。这适用于主对话框中的Winform独立应用程序 在理想的世界里,它似乎应该更简单。我希望代码打开Permissions.txt,我知道我正在成功访问它,因为消息框将显示列表中的第一个名称,并将环境.UserName与.txt文件中的所有名称进行比较。按钮显示后,将打开一个新对话框 任何愿意教新人的人。我已经搜索了一段时间,但我没有看到它 我也尝试过使

我是C#新手,我正在尝试基于txt文件的内容实现一个button.visible true/false。到目前为止,我所写的一切充其量都是不稳定的。这适用于主对话框中的Winform独立应用程序

在理想的世界里,它似乎应该更简单。我希望代码打开
Permissions.txt
,我知道我正在成功访问它,因为
消息框将显示列表中的第一个名称,并将
环境.UserName
.txt
文件中的所有名称进行比较。按钮显示后,将打开一个新对话框

任何愿意教新人的人。我已经搜索了一段时间,但我没有看到它

我也尝试过使用
File.Readlines
,但没有成功

提前感谢您愿意提供的任何帮助

弗兰克·皮特尔

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();

    }
编辑

还有,如果有人能解释一下弦线是如何形成的;正在设置为我的用户名。这就是它应该如何设置的,但我从未在原始代码中告诉过它line==newName。我想这就是
While
的目的。检查它们是否相等

最后编辑

这是我要做的工作。谢谢贝德福德

这一部分直接位于Form1类的下面

string[] lines = File.ReadAllLines(dataFolder + "\\Permissions.txt");
这是hideWidget()按钮背后的逻辑


我发布这个片段是为了让其他人从中受益。再次感谢贝德福德。这个新手真的很感谢你的帮助。哈格德!!:-)

您可以使用file.ReadAllLines静态方法读取文件中的所有行,然后使用LINQ查询检查是否有任何行与用户名匹配:

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;

应始终使用路径。组合
。在内部,它将确定参数是否以反斜杠开始/结束,并正确组合它们。@muffin Man I did'内部静态字符串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”);'我是新来的。我希望我正确地执行了这些内部操作来进行合并???@FrankPytel,我认为这些变量不需要
internal
修饰符。松饼人刚才说的是路径组合方法是如何工作的。@PredragDjukic Copy。我希望以后在代码中使用它们。
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;