- Home /
How to save a .txt file into a specified folder
Hi I'm trying to send and save information to a dedicated folder on my hard drive. Like a username for example.
I've actually been able to send the file out. But by default, it goes directly into the project folder.
Any way I can direct the file through a clear path like: Macintosh HD > Desktop > UsernameFolder ???
Any help would be great, thanks!
Answer by Imankit · Aug 24, 2017 at 06:18 AM
I think this method might help you
public static bool WriteFile (string path, string fileName, string data, FileMode mode)
{
bool retValue = false;
string dataPath = path;
try {
if (!Directory.Exists (dataPath)) {
Directory.CreateDirectory (dataPath);
}
dataPath += fileName;
try{
System.IO.File.WriteAllText (dataPath, data);
retValue = true;
} catch (System.Exception ex) {
string ErrorMessages = "File Write Error\n" + ex.Message;
retValue = false;
Debug.LogError (ErrorMessages);
}
return retValue;
}
Pass the path and file name you want and data in string format. Filename should contain the extension aswell
This is great. It needs a small tweak though:
public static bool WriteLine (string path, string fileName, string data)
{
bool retValue = false;
try {
if (!Directory.Exists (path))
Directory.CreateDirectory (path);
System.IO.File.WriteAllText (path + fileName, data);
retValue = true;
} catch (System.Exception ex) {
string Error$$anonymous$$essages = "File Write Error\n" + ex.$$anonymous$$essage;
retValue = false;
Debug.LogError (Error$$anonymous$$essages);
}
return retValue;
}
Your answer
Follow this Question
Related Questions
EditorUtility.OpenFilePanel uses \ FileInfo.Name uses / 0 Answers
How to write files to the streaming asset folder in Android at runtime 1 Answer
GetDirectories() does not work on Android 1 Answer
How to delete everything at Application.persistentDataPath? 3 Answers
StreamingAssets GIF images on android 0 Answers