Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


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

Terms of Use
Privacy Policy
gettempfilename (kernel32)
 
.
Summary

C# Signature:

[DllImport("kernel32.dll")]
static extern uint GetTempFileName(string lpPathName, string lpPrefixString,
   uint uUnique, [Out] StringBuilder lpTempFileName);

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

/// <summary>
/// The GetTempFileName and GetTempFolderName methods (and overloads) provide
/// the same level of convenience as the managed Path.GetTempFileName (including
/// support for folders), but provide access to the Win32 functionality of providing
/// an optional prefix, unique suffix and base folder name, as well as the Win32
/// functionality of creating the temp file or folder as soon as the name is generated.
/// </summary>
class Win32Utility {
  [DllImport("kernel32.dll", SetLastError = true)]
  static extern uint GetTempFileName(string lpPathName, string lpPrefixString, uint uUnique, [Out] StringBuilder lpTempFileName);

  public static string GetTempFileName() {
    return GetTempFileName("tmp");
  }

  public static string GetTempFileName(string prefix) {
    return GetTempFileName(prefix, 0);
  }

  public static string GetTempFileName(string prefix, uint unique) {
    return GetTempFileName(prefix, unique, Path.GetTempPath());
  }

  public static string GetTempFileName(string prefix, uint unique, string basePath) {

    // 260 is MAX_PATH in Win32 windows.h header
    // 'sb' needs >0 size else GetTempFileName throws IndexOutOfRangeException.  260 is the most you'd want.
    StringBuilder sb = new StringBuilder(260);    

    uint result = GetTempFileName(basePath, prefix, unique, sb);
    if( result == 0 ) {
      throw new Exception("Win32 Error: " + System.Runtime.InteropServices.Marshal.GetLastWin32Error());
    }

    return sb.ToString();
  }

  public static string GetTempFolderName() {
    return GetTempFolderName("tmp");
  }

  public static string GetTempFolderName(string prefix) {
    return GetTempFolderName(prefix, 0);
  }

  public static string GetTempFolderName(string prefix, uint unique) {
    return GetTempFolderName(prefix, unique, Path.GetTempPath());
  }

  public static string GetTempFolderName(string prefix, uint unique, string basePath) {
    string tempFolderName = GetTempFileName(prefix, unique, basePath);
    if( unique == 0 ) {
      // Delete created file and create folder name instead
      File.Delete(tempFolderName);
      Directory.CreateDirectory(tempFolderName);
    }

    return tempFolderName;
  }
}

Sample Code:

string tempFile = Win32Utility.GetTempFileName();
...
File.Delete(tempFile);

string tempFolder = Win32Utility.GetTempFolderName();
...
Folder.Delete(tempFolder);

Alternative Managed API:

System.IO.Path

[Visual Basic]
Public Shared Function GetTempFileName() As String

[C#]
public static string GetTempFileName();

[C++]
public: static String* GetTempFileName();

[JScript]
public static function GetTempFileName() : String;

Credits

Tips & Tricks: Chris Sells (csells@sellsbrothers.com)

Sample Code: Chris Sells (csells@sellsbrothers.com)

Documentation

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 PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions