-1

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

hndsmkkgr
  • 13
  • 5

2 Answers2

1

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.

RSF
  • 510
  • 6
  • 18
  • thanks for answering. I tried it, but it gave me another error message: UnauthorizedAccessException – hndsmkkgr May 30 '21 at 11:46
  • make sure IISUser has access to that path. best would be to have a folder created in "D" drive. E.g "D:\file-repo\" and give read and write access to IISUser - hope this helps to avoid UnauthorizedAccess exceptions/errors – RSF May 30 '21 at 11:59
  • Thank you so much for the reply! I finally managed to fix the problem by unchecking the "read only" checkbox in the folder properties – hndsmkkgr May 30 '21 at 12:01
0

After looking into various forums, I finally found my answer. Turns out you have to use an absolute path (like RSF said in the comments below) and uncheck the "read only" checkbox in the folder properties to avoid unauthorized access exception when you want to write into the file

hndsmkkgr
  • 13
  • 5