locked
Get computer name when not using authentication in a lightswicth desktop application RRS feed

  • Question

  • Hi,

    I have the need to record the computer name writing to the db in our lightswitch application. However, we aren't using any authentication in the application.

    Is there a way I can get the computer name through code?

    Thanks,

    Paul.


    • Edited by Paul Pitchford Wednesday, January 2, 2013 10:39 PM Spelling
    Wednesday, January 2, 2013 10:27 PM

Answers

  • In an normal .net assembly this would be very simple: Environment.MachineName. (forgive me if the syntax is wrong, i have no visual studio available currently)

    I'm afraid this is not available in silverlight.


    paul van bladel

    Thursday, January 3, 2013 1:45 PM

All replies

  • Where will this code be? In client code? Or in server code?

    Also, are you using VB or C#?


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Thursday, January 3, 2013 4:09 AM
    Moderator
  • Hi Yann,

    Client side preferably. I'm creating a proxy record when the user clicks a button.

    Thanks,

    Paul.

    Thursday, January 3, 2013 8:18 AM
  • In an normal .net assembly this would be very simple: Environment.MachineName. (forgive me if the syntax is wrong, i have no visual studio available currently)

    I'm afraid this is not available in silverlight.


    paul van bladel

    Thursday, January 3, 2013 1:45 PM
  • Hi Paul,

    Thanks for the feedback. To be honest, this is what I guessed from exploring the System.Enviroment class in Silverlight.

    Because I preempted that answer, I thought about maybe adding a txt file in a folder on each computer drive (c:\somedir\usr.txt) but I keep getting an IO.StreamReader FileNotFound exception even though the path is correct.

    This is the code:

    Private Function getUser() As String
    
                Dim user As String
                Dim file As FileInfo = New FileInfo("c:\ma\user.txt")
    
                Using reader As IO.StreamReader = file.OpenText
                    Dim inputLine As String = reader.ReadLine
                    user = inputLine
                    reader.Close()
                End Using
    
                Return user
    
            End Function

    Is this also some sort of restriction of Silverlight's inability to deal with the client OS?

    Basically, I just need a way of uniquely identifying a machine. It doesn't matter how or what it is, but I really could do with finding a solution here.

    Thanks for the help as always.

    Paul.

    Thursday, January 3, 2013 3:11 PM
  • @Paul - What about having code that runs on the Server (assuming this is a 2-tier deployment) that returns the name of the machine.  The Server would be running .NET code and then you could use Paul's suggestion Environment.MachineName.

    You could for example grab the machine name during the Entity's INSERTING method call.  If that would work for you...

    Let me know if you want more details.  I know this is a bit high level of an answer

    -Matt Sampson


    R. Matt Sampson

    Thursday, January 3, 2013 3:36 PM
  • Oops, I didn't see you mentioned clearly 2 tier deployment, sorry...

    Hi Matt,

    I didn't try it, but I would think the Environment.MachineName gives the hostName of the server, no?

    Not sure this is the intend of the OP.


    paul van bladel



    Thursday, January 3, 2013 3:45 PM
  • Yes it would.  But, depending on the setup, if his Server and Client machine are the same, I figured this would satisfy this scenario.

    R. Matt Sampson

    Thursday, January 3, 2013 3:47 PM
  • @Paul Pitchford - One other thing, for your file reading IO issues.  You can check out my blogs on reading file's from a SL app:

    http://blogs.msdn.com/b/rmattsampson/archive/2011/05/23/how-to-import-and-store-a-data-file.aspx

    http://blogs.msdn.com/b/rmattsampson/archive/2011/05/10/how-do-i-import-data-from-a-web-application.aspx

    You should definitely be able to do this with a SL app.

    Thx - Matt S


    R. Matt Sampson

    Thursday, January 3, 2013 4:09 PM
  • Hi Gents,

    It is the client's computer name I require and there are multiples of them. I'll take a look in detail at the blog Matt, hopefully just reading the text file will do. Although thinking about it, it may be less hassle just to implement some sort of windows authentication as they all log onto a domain controller anyway and then I could just query the logged in username which would suffice.

    Some more pondering to do! :)

    Thanks for the help guys, I appreciate it.

    Paul.


    Thursday, January 3, 2013 5:11 PM
  • UPDATE:

    It was so easy to implement the Windows Authentication I just went with it and it's now working a treat and a lot nicer than trying to access individual text files on 10-12 machines!

    Thanks for the help though, I think was a case of "couldn't see the wood for the trees!" Sometimes taking a step back and taking advice is really useful. :)

    Paul.

    Thursday, January 3, 2013 11:03 PM