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.
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.
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.
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?
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.