How to write the username in a local txt file when login success and check on file for next login?

ⅰ亾dé卋堺 提交于 2020-01-30 13:18:10

问题


I want to create a local txt file and store username after login success and for next login the app will check on this file if the username exists for bypass login. But it seems like no file created when I testing it

 bool bRet = ws.RequestMemberLogin(1, userName.Text, pass.Text, "", "");

 string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);

 string filename = System.IO.Path.Combine(path, "user.txt");

            FileInfo fi = new FileInfo(filename);

            bool exist = fi.Exists;
            if (bRet)
            {
                if (exist)
                {
                    // Read
                    using (StreamReader streamReader = new StreamReader(filename))
                    {
                        string content = streamReader.ReadToEnd();
                        System.Diagnostics.Debug.WriteLine(content);
                    }

                }
                else
                {
                    // Write
                    using (StreamWriter streamWriter = new StreamWriter(filename, true))
                    {
                        streamWriter.WriteLine(userName.Text);
                    }

                }

             Intent intent = new Intent(this, typeof(datalist));
             StartActivity(intent);

            }
            else
            {
                error.Text = "Invalid username or password!";

            }

回答1:


string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);

the path will like

"/data/user/0/packagename/files/user.txt"

As you save it to Internal Storage,you couldn't see the files without root permission,if you want to view it,you could use adb tool (application signed by Android Debug)

adb shell
run-as packagename
cd /data/data/packagename
cd files
ls

then you could see the user.txt

or you could save it to External storage,then you could find it in your device File Manager:

//"/storage/emulated/0/Android/data/packagename/files/user.txt"
string path = Android.App.Application.Context.GetExternalFilesDir(null).ToString();


来源:https://stackoverflow.com/questions/56966590/how-to-write-the-username-in-a-local-txt-file-when-login-success-and-check-on-fi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!