locked
Login User Details RRS feed

  • Question

  • Hi

       I want to save the data of a user who is logging in my machine with there credientials having the same domain.If he also logoff from my machine still the details of the user should be saved in a specific file.So that I can see the whole details when I will login in my machine.

    so can Any one help me in this in C#.

    Thanks

    fayaz

    Wednesday, June 20, 2012 7:34 AM

Answers

  • hi,

    you have some options here,

    1st and the easiest would be to use an *.txt file (maybe stored on some place like temp, or into project`s folder).

    2nd and a better option, would be to use an *.xml file.

    ---

    xml:

    1st create it. I would use an XDocument class and XElement class to specify all elements needed.

    2nd write into it, by using a Linq

    3rd read from it when needed.

    ---

    Code example:

    using System.IO;
    using System.Xml.Linq;
    
    string filePath = @"LoggedUser.xml";
    XDocument xDoc;
    
    //1. create:
    public void Create()
    {
        if(!File.Exists(filePath))  
        {
            xDoc = new XElement(
                XElement("Options",
                    XElement("UserData",
                        XElement("Username", ""),
                        XElement("Domain", ""))
                    ));
            xDoc.Save(filePath);
         }
    }
    
    //2. write into it:
    public void Write(string user, string domain)
    {
        xDoc.Load(filePath);
        var query = xDoc.Descendents("Options").Elements("UserData").Select(s=> new 
        {
            s.Element("Username").Value = user,
            s.Element("Domain").Value = domain;
            return s;
        }).ToList();
        query.Save(filePath);
    }
    
    //3. read:
    public List<string> Read()
    {
        xDoc.Load(filePath);
        return xDoc.Descendents("Options").Elements("UserData").Select(s=> new
    {
        USER = s.Element("Username").Value,
        DOMAIN = s.Element("Domain").Value
    }).ToList();
    }
    
    //to get the read data:
    List<string> data = Read();
    stirng name = list[0].USER;
    string domain = list[0]DOMAIN;

    Hope it helps,


    Mitja


    Wednesday, June 20, 2012 7:56 AM

All replies

  • hi,

    you have some options here,

    1st and the easiest would be to use an *.txt file (maybe stored on some place like temp, or into project`s folder).

    2nd and a better option, would be to use an *.xml file.

    ---

    xml:

    1st create it. I would use an XDocument class and XElement class to specify all elements needed.

    2nd write into it, by using a Linq

    3rd read from it when needed.

    ---

    Code example:

    using System.IO;
    using System.Xml.Linq;
    
    string filePath = @"LoggedUser.xml";
    XDocument xDoc;
    
    //1. create:
    public void Create()
    {
        if(!File.Exists(filePath))  
        {
            xDoc = new XElement(
                XElement("Options",
                    XElement("UserData",
                        XElement("Username", ""),
                        XElement("Domain", ""))
                    ));
            xDoc.Save(filePath);
         }
    }
    
    //2. write into it:
    public void Write(string user, string domain)
    {
        xDoc.Load(filePath);
        var query = xDoc.Descendents("Options").Elements("UserData").Select(s=> new 
        {
            s.Element("Username").Value = user,
            s.Element("Domain").Value = domain;
            return s;
        }).ToList();
        query.Save(filePath);
    }
    
    //3. read:
    public List<string> Read()
    {
        xDoc.Load(filePath);
        return xDoc.Descendents("Options").Elements("UserData").Select(s=> new
    {
        USER = s.Element("Username").Value,
        DOMAIN = s.Element("Domain").Value
    }).ToList();
    }
    
    //to get the read data:
    List<string> data = Read();
    stirng name = list[0].USER;
    string domain = list[0]DOMAIN;

    Hope it helps,


    Mitja


    Wednesday, June 20, 2012 7:56 AM
  • Hi Mitja

          Sorry for late Reply.Actually I did not get the whole thing.which to write in which class.Can you please clear me.

    Thanks

    fayaz

    Friday, June 22, 2012 1:00 PM
  • What you dont understand?

    Did you at least try to run the code?

    I cannot be more clear that I had been in the code above.

    --

    You just have to call all the method from your class (of form). You can create a new class to put the code from above into it, so it will be seperated from other code.

    Then you just call each of the methods:

    - Create();

    - Write(put your parameters inhere; my example only uses username and a domain) - so it would be like: Write("username1", "www.mydomain.com");

    - List<string> data = Read();


    Mitja

    Friday, June 22, 2012 1:17 PM