简体   繁体   中英

Read specific content from a file in c#

I want to retrieve the user name and the password from a .ser (Java Serialized Objects) file in

C# which I am saving into when I am creating a user.

ArrayList spnavn;
spnavn = FileController.ReadFile(Server.MapPath 
("~/App_Data/MinPlayers.ser"));  
Application.Set("Players", spnavn);

The user also has values like ID and such, but i only want to get the name and the password out, so the user can be able to use these as a login.

Can I do .getName or .getpassword in some way to get it out from the file? Or any other solutions ?

Basically I'm creating some users, via a webmasterpage and putting them into a file called MinPlayers.ser. So I want to get information out of it again, but the only information I want is the Name and Password which will be used in the loginform in my frontpage

public class FileController
{
    public static void Writefile(ArrayList a, string filnavn)
    {
        FileStream fs = new FileStream(filnavn, FileMode.Create, 

FileAccess.Write, FileShare.None);
        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(fs, a); // tag vores object, lav til binary 

string, og smid den videre
        fs.Close();
    }
    public static ArrayList ReadFile(string filnavn)
    {
        FileStream fs = new FileStream(filnavn, FileMode.Open, 

FileAccess.Read);
        BinaryFormatter bf = new BinaryFormatter();
        ArrayList a = (ArrayList)bf.Deserialize(fs);
        fs.Close();
        return a;

    }

}

http://msdn.microsoft.com/en-us/library/e474a3yk.aspx

if you take a look at the link I posted: I want to forexample get customerAddress.Region and customerAddress.City out of the file instead of the WHOLE string with all values if you understand?

customerAddress.Street = "1111 White Street";
customerAddress.City = "Sturtevant";
customerAddress.Region = "WI";
customerAddress.PostalCode = "53177";
customerAddress.CacheAddress();

First of all, you don't come up with a Java Serialized Object when you use the .NET BinaryFormatter.Serialize method, because you're surely not developing in J#. So the MSDN link you provide is irrelevant.

With the code you give, and providing you have some sort of Serializable User class:

[Serializable]
public class User
{
  public string id;
  public string pwd;
  public string someOtherInterestingStuff;
}

this should work just fine:

public void Test()
{
  ArrayList a = new ArrayList();
  a.Add(new User() {id = "001", pwd = "pass1"});
  a.Add(new User() {id = "002", pwd = "pass2"});
  Writefile(a, "testusers.bin");
  a = ReadFile("testusers.bin");
  User user = (User) a[0];
  Console.WriteLine(user.id + " " + (user.pwd);
}

The other properties are still there, but you don't have to use it, so this should not be a problem. Or is it? If the answer is yes, check this .

Be aware that storing logins/passwords this way (even in a binary form) is not secure. Consider using something else (eg ASP.NET Identity...).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM