What is the path to read/write files on the desktop?

I need to first of all detect whether there is a file present on the user’s desktop, in both Windows and Mac. This would be to detect the pre-existence of a screenshot e.g. grab.png

Then I need to write a file (a new screengrab) to the desktop (it may have a different file name depending on whether the standard filename exists already, ie I might append a number to the end of the name, or increment the existing number).

So how do I get the desktop path on Windows/Mac, and is it possible to do reads/writes to the desktop from Unity without access/permission problems?

use System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)

Although I do suggest creating a folder in the user’s My Documents instead and putting the screenshots there.

Web release of Unity programs can’t access the hard drive due to security considerations, so it can’t read/write to the desktop, but standalone Unity programs can.

3 Likes

What would be the path to get to the user’s documents folder?

And does this work on both windows and osx the same?

Maybe that : Unity - Scripting API: FileUtil.GetUniqueTempPathInProject

I don’t think so, that’s a temp folder or something inside the project folder, I need to get to the user’s my documents folder on windows (like c:\documents and settings\admin\my documents\ or whatever), and users/username/documents on osx.

the userfolder can also be extracted through System.Environment

Ahh you mean like using these keywords instead of Desktop:

One last question, does it work exactly the same without any modifications on OSX? Can I use SpecialFolders.Desktop, SpecialFolders.MyPictures, SpecialFolders.MyDocuments etc?

Ok so I tested this and it works fine on OSX, the only difference is that you have to make sure to use forward slashes in OSX pathnames, whereas windows requires backward slashes.

windows since Win2k does accept forward slashes too and as you will work on xp+ only anyway, thats no problem.

System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)

This will work for all OS’s that mono supports, which includes windows and mac.

Windows supports both backward and forward slashes. Generally it is safer to use forward slashes and just forget about using backward slashes.

System.Environment.GetFolderPath returns back slashes on vista, while osx permits a backward slash as part of a file or folder name, so how can i deal with both in a cross platform way? is there a compiler directive to run platform specific code?

no there is no directive for any kind of platform other than the “addon platform” distinction ie #UNITY_IPHONE

but you don’t need that cause you can just ask it for which platform it is running on. Application.platform isn’t there just for fun :wink:

Oh, I didn’t know about Application.platform, I’ll have to check it out, thanks.

Since Windows Vista (for me anyway) seems to be returning the path with back slashes, does anyone know if this will always be the case across other Windows versions? Do I need to detect the slash direction or can I just rely on it always coming in as back slashes?

This is a useful (but old) topic, and after reading the issue, this could also contribute to my current issue as well.

I could start a new thread, but I believe my issue involves some of the same headaches as “imaginaryhuman” was having.

I am getting crashes in my windows standalone due to either the way I am writing the File, or the string path contents (possibly the slashes based on the windows operating system).

The problem I am having is the app crashes on Windows 8.1 when it gets to the File.AppendAllText line.

#if UNITY_STANDALONE_OSX
            if (!PlayerPrefsX.GetBool("datetime"))
                System.IO.File.AppendAllText(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "/Desktop/" + GameManager.I.selectedGame + ".txt", dateTime + System.Environment.NewLine + System.Environment.NewLine);

            System.IO.File.AppendAllText(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "/Desktop/" + GameManager.I.selectedGame + ".txt", numbers);
#endif
#if UNITY_STANDALONE_WIN
            if (!PlayerPrefsX.GetBool("datetime"))
                System.IO.File.AppendAllText(System.Environment.GetFolderPath( (System.Environment.SpecialFolder.Desktop) ) + "/" + GameManager.I.selectedGame + ".txt", dateTime + System.Environment.NewLine);

            System.IO.File.AppendAllText(System.Environment.GetFolderPath( (System.Environment.SpecialFolder.Desktop) ) + "/" + GameManager.I.selectedGame + ".txt", numbers + System.Environment.NewLine);
#endif

It would be nice if I could simply use the call rpovided, and not have to worry so much as it conflicts with different windows operating systems.

Thanks for any help

Larry