- Home /
Why do I get the error - 'WriteAllText' is not a member of 'System.IO.File
I am trying to create a simple text file but I'm getting the above error.
All I am doing is the following:
System.IO.File.WriteAllText("C:/Users/Simon/Desktop/exampleSaveData.txt", stringToPost);
This post is related.
Have you included System.IO at the top of your file? Are you sure you have spelled everything correctly?
I have import System; at the top of the script. I am using Javascript. This is the correct syntax right?
Answer by arkon · Oct 05, 2011 at 10:57 AM
Check your game target, I had the same problem. You can't use WriteAllText if the target is the web. There doesn't seem to be a unity stub for it either so you need to @ifdef it out on web targets.
That's exactly what he's saying. :) Think about it for a second - Webplayers are executed on the user's own machine, like Flash and Silverlight applications are. How would you like programmers to be able to write programs that you download in a website and execute locally, which put whatever files they want at whatever location they want, on your computer?
I could write a program that executes the following:
int count = 0;
while(true) { System.IO.WriteAllText("C:/Users/Simon/Desktop/exampleSaveData" + count+".txt", "Hahahahaha, screw you, this is a billion files on your desktop"); count++; }
Would you like to run that? ;) That functionality is limited in a web context is a very, very, VERY good thing.
Yeah that works cheers. I switched to standalone to extract my text file.
Thanks Christian, I understand that but as I'm running through the editor locally I assumed that wouldn't be a problem or perhaps give me some kind of useful warning/error. But I know now.
I don't $$anonymous$$d the fact it's not there but there should be a stub, so the code compiles and maybe just outputs a message too tell you its not implemented. I hate littering my code with #if defs for this reason.
Answer by drudiverse · Aug 24, 2015 at 09:28 AM
Ok switch to standalone and not web player mode fixes it...
I just had the error happening in one script and not another with the same line.
Your answer
Follow this Question
Related Questions
How do you create a folder in c#? 3 Answers
Trouble creating a text file 4 Answers
How can I get game data from a specified file? 0 Answers
Create GUIText from Javascript 3 Answers