Search code examples
c#terminal-services

How can a Terminal Services aware application retrieve the user's private Windows directory?


The documentation for GetWindowsDirectory says:

  • TS-Aware applications get the actual Windows directory.
  • Non-TS-Aware applications get the current user's "private" Windows directory.

So, I am working on a C# class library that needs to be TS-Aware, because I cannot require all apps using this library to be non-TS-Aware. The C# default is to be TS-Aware.

I need to drop INI files into the Windows directory to communicate with a legacy App that is NOT TS-Aware. This works without difficulty in a regular user, but not in a Terminal Services session.

Under Terminal Services:

  • The Legacy App uses the INI file in the user's "private" Windows directory.
  • The C# library uses the INI file in the system Windows directory.

I need to:

  1. Detect when running under Terminal Services. I can do this by checking the SystemInformation.TerminalServerSession flag.
  2. From a TS-Aware process, locate the user's "private" Windows directory. I can find no documentation that tells me how to do this.

Solution

  • I think it is always under the user folder in the windows sub directory, so:

    System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "windows");