windir on a remote machine

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hayato Iriumi

    #1

    windir on a remote machine

    Hello, folks.
    I have a need to get the value of %windir% on a remote machine. Is it
    possible to get this value using C#?

  • Peter Duniho

    #2
    Re: windir on a remote machine

    Hayato Iriumi wrote:
    Hello, folks.
    I have a need to get the value of %windir% on a remote machine. Is it
    possible to get this value using C#?
    I'm not aware of any API that provides that directly, even outside of
    ..NET. However, I would think that any sort of remoting API that allows
    you to execute code on a remote machine (however you choose to implement
    it) would allow you to use the Environment.Get EnvironmentVari able()
    method to get the information you want.

    Pete

    Comment

    • Larry Smith

      #3
      Re: windir on a remote machine

      Hello, folks.
      I have a need to get the value of %windir% on a remote machine. Is it
      possible to get this value using C#?
      Don't know if there's a higher level function that will do this but the
      solution seems to be "RegistryKey.Op enRemoteBaseKey ()". Just read the value
      from "HKEY_LOCAL_MAC HINE\SYSTEM\Cur rentControlSet\ Control\Session
      Manager\Environ ment". At least that seems to be the analogue for how you
      would do it using the native WinAPI (which I have done in C++ using
      "RegConnectRegi stry()"). Note that you have authorization issues to deal
      with of course but this will likely be a non-issue if the security context
      of the calling thread has access to the other machine (if not you can
      authenticate in code but that's another story).


      Comment

      • Hayato Iriumi

        #4
        Re: windir on a remote machine

        Hi, Larry. Thanks for your reply. I really appreciate your help!

        On Aug 22, 12:33 pm, "Larry Smith" <no_spam@_nospa m.comwrote:
        Hello, folks.
        I have a need to get the value of %windir% on a remote machine. Is it
        possible to get this value using C#?
        >
        Don't know if there's a higher level function that will do this but the
        solution seems to be "RegistryKey.Op enRemoteBaseKey ()". Just read the value
        from "HKEY_LOCAL_MAC HINE\SYSTEM\Cur rentControlSet\ Control\Session
        Manager\Environ ment". At least that seems to be the analogue for how you
        would do it using the native WinAPI (which I have done in C++ using
        "RegConnectRegi stry()"). Note that you have authorization issues to deal
        with of course but this will likely be a non-issue if the security context
        of the calling thread has access to the other machine (if not you can
        authenticate in code but that's another story).

        Comment

        • Tom Porterfield

          #5
          Re: windir on a remote machine

          Larry Smith wrote:
          >Hello, folks.
          >I have a need to get the value of %windir% on a remote machine. Is it
          >possible to get this value using C#?
          >
          Don't know if there's a higher level function that will do this but the
          solution seems to be "RegistryKey.Op enRemoteBaseKey ()". Just read the value
          from "HKEY_LOCAL_MAC HINE\SYSTEM\Cur rentControlSet\ Control\Session
          Manager\Environ ment". At least that seems to be the analogue for how you
          would do it using the native WinAPI (which I have done in C++ using
          "RegConnectRegi stry()"). Note that you have authorization issues to deal
          with of course but this will likely be a non-issue if the security context
          of the calling thread has access to the other machine (if not you can
          authenticate in code but that's another story).
          As long as the remote machine is a newer version of Windows this should
          work. Non NT based Windows (Windows 95, Windows 98) do not store
          environment variables in the registry.
          --
          Tom Porterfield

          Comment

          • Larry Smith

            #6
            Re: windir on a remote machine

            As long as the remote machine is a newer version of Windows this should
            work. Non NT based Windows (Windows 95, Windows 98) do not store
            environment variables in the registry.
            Yes, there are some caveats (the "Remote Registry" service must be running
            on both machines for instance) but the above issue is probably the least of
            his worries. I don't recall if "windir" is even defined on those (almost
            antiquated) OSs. In any event, reading values like this from the registry is
            rather ugly IMO (though probably stable in this particular case). Moreover,
            since "windir" is the equivalent of CSIDL_WINDOWS using the WinAPI shell
            functions such as "SHGetFolderPat h()", he might want to investigate if a
            dedicated function exists for finding special folders on a remote machine
            (assuming this is his real objective)


            Comment

            • Willy Denoyette [MVP]

              #7
              Re: windir on a remote machine

              "Larry Smith" <no_spam@_nospa m.comwrote in message
              news:eYQvYNP5HH A.1148@TK2MSFTN GP05.phx.gbl...
              >Hello, folks.
              >I have a need to get the value of %windir% on a remote machine. Is it
              >possible to get this value using C#?
              >
              Don't know if there's a higher level function that will do this but the
              solution seems to be "RegistryKey.Op enRemoteBaseKey ()". Just read the
              value from "HKEY_LOCAL_MAC HINE\SYSTEM\Cur rentControlSet\ Control\Session
              Manager\Environ ment". At least that seems to be the analogue for how you
              would do it using the native WinAPI (which I have done in C++ using
              "RegConnectRegi stry()"). Note that you have authorization issues to deal
              with of course but this will likely be a non-issue if the security context
              of the calling thread has access to the other machine (if not you can
              authenticate in code but that's another story).
              >

              This isn't of great help has the value stored is a placeholder
              (%SystemRoot%) which gets expanded by the OS before it's returned in the
              environment block of the logon user, you will have to expand this on the
              remote system to get it's real value. Much easier is to use
              System.Manageme nt to get at this kind of info.
              Willy.

              Comment

              • Willy Denoyette [MVP]

                #8
                Re: windir on a remote machine

                "Hayato Iriumi" <hiriumi@gmail. comwrote in message
                news:1187809072 .730350.144350@ m37g2000prh.goo glegroups.com.. .
                Hello, folks.
                I have a need to get the value of %windir% on a remote machine. Is it
                possible to get this value using C#?
                >


                Use System.Manageme nt and read the "WindowsDirecto ry" property of WMI's
                class Win32_Operating System.
                Next sample reads the "windowsdirecto ry" from a remote server (BOBSMachine)
                ....
                ConnectionOptio ns co = new ConnectionOptio ns();;
                co.Username = "administrator" ; // user with sufficient privileges to
                connect to the cimv2 namespace
                co.Password = "adminPwd"; // his password
                ManagementScope scope = new
                ManagementScope (@"\\BOBSMachin e\root\cimv2", co);
                SelectQuery query =
                new SelectQuery("Se lect windowsdirector y from
                Win32_Operating System");
                ManagementObjec tSearcher searcher =
                new ManagementObjec tSearcher(scope , query);
                foreach (ManagementObje ct windir in searcher.Get())
                Console.WriteLi ne("Value = {0}", windir["windowsdirecto ry"]);
                ....

                Willy.

                Comment

                • Larry Smith

                  #9
                  Re: windir on a remote machine

                  This isn't of great help has the value stored is a placeholder
                  (%SystemRoot%) which gets expanded by the OS before it's returned in the
                  environment block of the logon user, you will have to expand this on the
                  remote system to get it's real value. Much easier is to use
                  System.Manageme nt to get at this kind of info.
                  Willy.
                  Alas you're correct but I didn't know what native .NET methods were
                  available to retrieve this info (which he should normally depend on of
                  course). However, it was really a direct answer to his question though I'm
                  still not 100% certain of his intentions (though I should have clarified
                  it). He may literally require the value of this environment variable in its
                  raw or expanded state. This could even differ from %SYSTEMROOT% in theory
                  (and the value returned by your own example) so he needs to confirm what
                  he's really looking for, the value of "windir" (expanded or otherwise), or
                  the path of the windows folder regardless of "windir". In any case, I didn't
                  take the time to look at what the default value for "windir" was (in terms
                  of other environment variables) since he could normally plug in any other
                  environment variables in as required. This would involve some extra work to
                  parse "windir" and expand any other embedded variables but it's a fairly
                  trivial exercise. "%SystemRoo t% however presents an impediment since it's
                  one of several predefined environment variables that isn't stored as a
                  regular environment variable. Its actual value would therefore need to be
                  (remotely) tracked down but it's potentially doable (I'd have to look). The
                  logged on user on the remote machine is another matter however. He shouldn't
                  depend on any expansion associated with that user via the remote equivalent
                  of "ExpandEnvironm entStrings()" and cousin(s). "windir" is a system
                  environment variable so it shouldn't be defined in terms of user environment
                  variables. There may not even be any interactive user at the time or call or
                  it's even possible there could be more than one interactive logon session
                  (even without any human actually being logged on in theory).


                  Comment

                  Working...