Search code examples
c#unity-game-enginestreamreaderstreamwritertxt

How to read and write a txt file outside of project folder (Unity)


I tried using File, StreamWriter and StreamReader with a path outside of my project folder but it didn't work. It became projectPath\externalPath. For example my project path is F:\Project\ and my txt file is in D:\File.txt, Unity automatically read my path as "F:\Project\D:\File.txt", or it gives an error message: UnauthorizedAccessException

I tried using WWW too, but I got an error message "cannot convert WWW to string"

please help


Solution

  • Try giving an absolute path instead of a relative path. e.g

    var fullPath = "D:\<file-name>.txt";
    var content = "testing"; 
    File.WriteAllText(fullPath, content );
    

    Above should write the file in your "D:" drive.