Desktop Functions:

   Smart Device Functions:

Show Recent Changes
Subscribe (RSS)
Misc. Pages
Helpful Tools
Suggested Reading
Website TODO List
Download Visual Studio Add-In

GetTempPath (kernel32)
Retrieves the path of the directory designated for temporary files.

C# Signature:

static extern uint GetTempPath(uint nBufferLength,
   [Out] StringBuilder lpBuffer);

VB Signature:

Declare Function GetTempPath Lib "kernel32.dll" (nBufferLength As Integer, _
   <Out> lpBuffer As StringBuilder) As Integer



Tips & Tricks:

Please add some!

Sample Code:

VB Sample

'(DllImport declarations omitted)
Public Class TempFile
     Public Function Create(ByVal astrFoldername As String, _
                ByVal astrPrefix As String) As String
     Dim tempPath As New StringBuilder(MAX_PATH - 14)
     Dim tempName As New StringBuilder(MAX_PATH)
     Dim lngRet As Integer
     Dim result As String = ""

     lngRet = 1

     If (astrFoldername = "") Then
         lngRet = GetTempPath(MAX_PATH - 14, tempPath)
         astrFoldername = tempPath.ToString
     End If

     If (lngRet > 0 And lngRet <= MAX_PATH - 14) Then
         lngRet = GetTempFileName(astrFoldername, astrPrefix, 0, tempName)
         If lngRet <> 0 Then
         result = tempName.ToString
         End If
     End If

     Return result
   End Function

End Class

'This project references Microsoft.VisualStudio.QualityTools.UnitTestFramework
Imports System.IO
Imports System.Text

Imports Microsoft.VisualStudio.TestTools.UnitTesting

'''This is a test class for TempFileTest and is intended
'''to contain all TempFileTest Unit Tests
<TestClass()> _
Public Class TempFileTest
    Private fFoldername As String = "C:\TempFile"
    Private fPrefix As String = "Prefix"  ' Only first three characters will be used by Windows API
    Private testContextInstance As TestContext

    '''Gets or sets the test context which provides
    '''information about and functionality for the current test run.
    Public Property TestContext() As TestContext
        Return testContextInstance
    End Get
    Set(ByVal value As TestContext)
        testContextInstance = value
    End Set
    End Property

    '''A test for Create
    <TestMethod()> _
    Public Sub CreateTest()
    Dim target As TempFile = New TempFile
    Dim expected As String = fFoldername + "\" + Left(fPrefix, 3)  'Only part of the expected answer. The other part is random.
    Dim actual As String
    Dim dirInfo As DirectoryInfo

    If Not Directory.Exists(fFoldername) Then
        dirInfo = Directory.CreateDirectory(fFoldername)
        If Not dirInfo.Exists Then
        Throw New Exception("Directory '" + fFoldername + "' can not be created")
        End If
    End If

    actual = target.Create(fFoldername, fPrefix)
    'Remove file before procedure exits.
    Assert.AreEqual(Left(expected, Len(fFoldername) + 4), Left(actual, Len(fFoldername) + 4))

    Dim shortPath As New StringBuilder(TempFile.MAX_PATH)
    TempFile.GetShortPathName(Path.GetTempPath, shortPath, TempFile.MAX_PATH)
    expected = shortPath.ToString + Left(fPrefix, 3) ' GetTempPath end with "\"
    actual = target.Create("", fPrefix)
    'Remove file before procedure exits.
    Assert.AreEqual(Left(expected, Len(fFoldername) + 4), Left(actual, Len(fFoldername) + 4))
    End Sub
End Class

Alternative Managed API:


GetTempPath on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

Access directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version