Search code examples
c#windows-store

Path.GetTempPath() not available in Windows Store Apps


I am trying to get the temp directory by using:

string tempFolder = System.IO.Path.GetTempPath();

However that method does not exist. I can see all the other methods in intelliSense though.

Why is that method not available. Is there another way to get the temp folder location in a Windows Store app?


Solution

  • Create the temporary file in your ApplicatonData storage. You will have to generate your own filename with a guid or timestamp.

    Windows.Storage.StorageFolder temporaryFolder = ApplicationData.Current.TemporaryFolder;
    StorageFile sampleFile = await temporaryFolder.CreateFileAsync("dataFile.txt", CreationCollisionOption.ReplaceExisting);
    await FileIO.WriteTextAsync(sampleFile, formatter.Format(DateTime.Now));
    

    A Windows Store App is sandboxed, so you are expected to read and write to folders inside your sandboxed application folder. You won't be able to write to the traditional temp folder C:\Windows\TEMP, as you probably want, and yeah you are out of luck. There are a few other locations outside of your application folder that you have access to, but in most cases your access is limited.

    The KnownFolders class is how you access the following locations.

    • CameraRoll
    • DocumentsLibrary
    • HomeGroup
    • MediaServerDevices
    • MusicLibrary
    • PicturesLibrary
    • Playlists
    • RemovableDevices
    • SavedPictures
    • VideosLibrary

    KnownFolders class on MSDN