第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

找不到 txt 文件 StreamReader

找不到 txt 文件 StreamReader

C#
至尊寶的傳說 2021-08-22 15:28:01
'未處理的異常:System.IO.FileNotFoundException: 無法找到文件“/Logins.txt”發(fā)生'嗨,新手在 xamarin c# 中制作了一個非?;镜牡卿?注冊系統(tǒng),它使用來自 Systsem.IO 的 StreamReader 讀取帶有存儲的用戶名和密碼的文本文件。txt 文件與 .cs 文件本身位于同一目錄中,并且在解決方案資源管理器中可見。我試過輸入完整路徑但沒有成功,并以管理員身份運(yùn)行,以防萬一它與權(quán)限有關(guān)。有什么不對嗎?public partial class LoginPage : ContentPage{    public LoginPage()    {        InitializeComponent();    }    List<string> user = new List<string>();    List<string> pass = new List<string>();    public void btnLogin_Clicked(object sender, EventArgs e)    {        //Read the txt        StreamReader sr = new StreamReader("Logins.txt");        string line = "";        //Read every line until there is nothing in the next line        while ((line = sr.ReadLine()) != null)        {            //Grab items within new lines separated by a space and chuck them into their array            string[] components = line.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);            user.Add(components[0]);            pass.Add(components[0]);        }        //Check entries are on the same line and match        if (user.Contains(txtUsername.Text) && pass.Contains(txtPassword.Text) && Array.IndexOf(user.ToArray(), txtUsername.Text) == Array.IndexOf(pass.ToArray(), txtPassword.Text))        {            Navigation.PushModalAsync(new HomeScreen());        }        else            DisplayAlert("Error", "The username or password you have entered is incorrect", "Ok");    }    private void Cancel_Clicked(object sender, EventArgs e)    {        Navigation.PushModalAsync(new MainPage());    }}
查看完整描述

1 回答

?
qq_遁去的一_1

TA貢獻(xiàn)1725條經(jīng)驗 獲得超8個贊

如果將Logins.txt文件更改為嵌入資源,則可以在運(yùn)行時從程序集中加載該資源,如下所示:


var assembly = IntrospectionExtensions.GetTypeInfo(typeof(LoginPage)).Assembly;

Stream stream = assembly.GetManifestResourceStream("YourAssembly.Logins.txt");

string text = "";

using (var reader = new System.IO.StreamReader (stream)) 

{

    text = reader.ReadToEnd();

}

YouAssembly指的是構(gòu)建項目時將生成的程序集 (dll) 的名稱。如果你不知道這是什么,或者沒有改變它,它可能與項目同名。


Xamarin 文檔有一個很好的例子來說明這是如何完成的:Xamarin.Forms 中的文件處理


編輯:


在構(gòu)造函數(shù)中執(zhí)行此操作并處理內(nèi)容以便您可以進(jìn)行身份驗證處理的更完整示例。


但首先,讓我們定義一個 POCO 來保存用戶名/密碼的詳細(xì)信息:


public class UserCredentials

{

    public string Username {get;set;}

    public string Password {get;set;}

}

添加一個屬性來保存這個類:


List<UserCredentials> CredentialList {get;set;}

現(xiàn)在,在構(gòu)造函數(shù)中:


public LoginPage()

{

    InitializeComponent();


    // Read in the content of the embedded resource, but let's read 

    // each line to make processing a little easier

    var assembly = IntrospectionExtensions.GetTypeInfo(typeof(LoginPage)).Assembly;

    Stream stream = assembly.GetManifestResourceStream("YourAssembly.Logins.txt");

    var textLines = new List<string>();

    string line = null;

    using (var reader = new System.IO.StreamReader (stream)) 

    {

        while ((line = sr.ReadLine()) != null) 

        {

            textLines.Add(line);

        }

    }


    // Init our cred list

    this.CredentialList = new List<UserCredentials>();


    // Read out the contents of the username/password combinations

    foreach (var line in textLines)

    {

        // Grab items within new lines separated by a space and chuck them into their array

        string[] components = line.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);


        // Add the combination to our list

        this.CredentialList.Add(new UserCredential 

        {

            Username = user.Add(components[0]),

            Password = pass.Add(components[0])

        });

    }

}

現(xiàn)在我們的登錄變得容易多了。以前,我們首先檢查用戶名/密碼是否都存在于我們的已知數(shù)據(jù)集中,然后檢查它們在兩個列表中是否位于相同的索引處。


public void btnLogin_Clicked(object sender, EventArgs e)

{

    // Now can use linq to validate the user

    // NOTE: this is case sensitive

    if (this.CredentialList.Any(a => a.Username == txtUsername.Text && a.Password == txtPassword.Text))

    {

        // NOTE: you've only validated the user here, but aren't passing

        // or storing any detail of who the currently logged in user is

        Navigation.PushModalAsync(new HomeScreen());

    }

    else

    {

        DisplayAlert("Error", "The username or password you have entered is incorrect", "Ok");

    }

}


查看完整回答
反對 回復(fù) 2021-08-22
  • 1 回答
  • 0 關(guān)注
  • 370 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號