Kevin Gale
Kevin Gale

Reputation: 4468

Find temp folder for user Network Service?

I'm trying to write a wrapper to help our customers restore a SQL Server database. By default SQL Server runs as user Network Service which has very limited permissions. Our customers are sometimes confused that they can't just point to a backup file in any location and restore it.

I want to copy the file from the location they choose to a temp location that SQL Server can access but I'm having a hard time finding a location. I can't just call Path.GetTempPath() because that gives me the user's temp which SQL Server can't access.

Is there a folder I can retrieve that will always work?

Upvotes: 27

Views: 30231

Answers (3)

Kishore A
Kishore A

Reputation: 1385

On Windows Vista and above

  1. System Account (NT AUTHORITY\SYSTEM)
    C:\Windows\TEMP

  2. Network Service (NT AUTHORITY\NETWORK SERVICE) C:\Windows\ServiceProfiles\NetworkService\AppData\Local\Temp

  3. Local Service (NT AUTHORITY\LOCAL SERVICE) C:\Windows\ServiceProfiles\LocalService\AppData\Local\Temp

Upvotes: 6

Helge Klein
Helge Klein

Reputation: 9095

The temp folder for the Network Service account is located here:

%Windir%\ServiceProfiles\NetworkService\AppData\Local\Temp

On Windows XP/2003 the network service profile is located in the general user profile directory. On a German machine it is here:

C:\Dokumente und Einstellungen\NetworkService\Lokale Einstellungen\Temp

On an English computer it would be here:

C:\Documents and Settings\NetworkService\Local Settings\Temp

You can find the path to the profile on all Windows versions by querying this registry value:

HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-20\ProfileImagePath

That does not give you the localized name of "Local Settings" on XP/2003, though.

By default, Administrators can write to the network service profile. Using it's temp folder for your purpose should be perfectly OK.

Upvotes: 41

Justin Dearing
Justin Dearing

Reputation: 14978

Why not just give NETWORK_SERVICE permission to a specific folder and tell the users to place the backup files there?

Or just run:

BACKUP DATABASE [master] TO DISK='master.bak'
GO

from SSMS and see where the file gets written to.

Upvotes: 0

Related Questions