7

I have a Web Service that calls another Executable which fetches a list of files and stores them at a temporary location, which will in turn be read by the web service.

I thought i will use the system's temp folder and found out that i could use System.IO.Path.GetTempPath function to get the temp folder and store my files here. But when i checked the output returned by this function it gave me

C:\Users\username\AppData\Local\Temp\15\

i am worried about the \15 at the end of the path. Does this mean that the temp folder returned by GetTempPath is not constant and keeps changing? I need it to be constant since i need the web service to read from the temp files output by the executable

Sadhir
  • 413
  • 4
  • 13
  • 3
    At the point when you save the file, don't just store the filename, store the whole path. Then it will not matter if it changes. – Ben Jun 26 '12 at 16:21
  • How can you control where the other executable writes the files? Would it be possible to do what the executable does without writing files to disk in the first place? Is the executable run within te same identity? – Jodrell Jun 26 '12 at 16:24
  • To be more specific... The Web service calls the executable with an id as the first parameter. My idea was that the executable will create a file that has id as part of the name (say MyService_.jpg). The Web service will then read this file from the temp folder (since it knows the id). So, @Ben i am not storing the filename or the path anywhere – Sadhir Jun 26 '12 at 16:29
  • 1
    Are they both running as the same user? you dont normally see a service account's tmp under c:\users, why not add your own "MY_SHARED_PATH" environmental variable and .GetEnvironmentVariable it – Alex K. Jun 26 '12 at 16:32
  • @Jodrell - the executable will be launched by the web service so i assume it will use the same identity. the executable fetches an image for the given id and this could be re-used in other projects i will be working on in the future. so writing to a file is the only way i can think of for the executable to communicate with the WS. – Sadhir Jun 26 '12 at 16:34
  • 1
    @Sadhir, why not pass the whole path to the executable then the webservice can control where the file is created. – Ben Jun 26 '12 at 16:46
  • @Ben - yeah that's a better solution. I think i will follow that approach. Thanks! – Sadhir Jun 26 '12 at 16:51

2 Answers2

3

The documentation says:

This method checks for the existence of environment variables in the following order and uses the first path found:

1.The path specified by the TMP environment variable.

  1. The path specified by the TEMP environment variable.

  2. The path specified by the USERPROFILE environment variable.

  3. The Windows directory.

So unless you change your environment the result is stable.

(Environment changes can occur, if you run as a service account, which never did an interactive logon), which has a default %SystemRoot%\Temp folder. If someone logs on to the server with this service account, a profile is created and the temp path will change)

Community
  • 1
  • 1
Joey
  • 344,408
  • 85
  • 689
  • 683
  • @Erik: Will the environment variable actually change for your process? I thought you'd have to explicitly process a certain window message to get notified when the environment changes and no one will actually do that for your process from the outside. – Joey Jul 03 '18 at 10:52
  • I think that when an account never logged in/doesn't have a profile no Path is being set, so you end up in step 4 (the Windows Directory). Only when the profile is created and you restart the process the new environment variables will show up – Erik Oppedijk Jul 04 '18 at 18:28
2

Even if this post is somewhat older, I would like to share knowledge :-)

When running on a Terminalserver it is the default behavior of Windows to create a separate temporary subfolder for every connection to this server. There are ways to affect this but before you want to do this I would recommend to place your files somewhere else.

(http://blogs.msdn.com/b/oldnewthing/archive/2011/01/25/10119675.aspx)

AlexS
  • 344
  • 4
  • 14