I am getting this error System.IO.File' does not contain a definition for ReadAllBytes’ . It is a function in .NETand I have installed Unity3d 3.4.0f5 (twice) .I have API compatibility set .NET 2.0 and stripping level disabled in player settings. Is this just a problem with my install or is FILE.ReadAllBytes() really not there?
using System.IO;
at the top so the assembly is present in the build and not cut by auto build size optimizations beyond stripping, as System.* are handled special due to their build sizes and impact on linking
using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System;
I tried System.IO.File but is it is a type not namespace that produced an error.
since easysave is on the asset store’s top 10 scripting list I doubt it is error in their code. Maybe it is some sort of interaction with other code. I’ll try in clean project. I see that this is the only use of any System.IO.File methods in the easysave code.
Corrupted project file was the problem. I exported all assets to a package and opened in new project and it was OK. Now just have to
-add layers and
-tags
-layer collision matrix
and then test. The project had been upgraded to various 3.4 betas 3.3 so probably somewhere in there or during one of my OS crashes (unrelated to Unity).
Deleted all Unity files I could find. including application/unity ~/Library/Preferences/unity3d… and ~/Library/Preferences/com.unity… files. redownloaded and reinstalled unity. started unity and switched platform to web. System.IO.File' does not contain a definition for ReadAllBytes’ again
Surprised no one brought this up before - odds are you can’t use ReadAllBytes in the webplayer.
Edit - just tested it my end and got expected result - failing to compile for webplayer.
using UnityEngine;
public class BugTest : MonoBehaviour
{
void Start()
{
#if UNITY_WEBPLAYER
print("Not Going To Read That File!");
#else
//print(Application.dataPath );
var bytes = System.IO.File.ReadAllBytes(Application.dataPath + "/test.txt");
string result = "";
foreach (var b in bytes)
result += (char)b;
print(result);
#endif
}
}
I’ve been search around more and I found threads saying that you can not read/write external files from web player. So I don’t if I would call it a bug but I will as easysave about putting #if UNITY_WEBPLAYER lines in their code.
This thread helped a bunch. I didn’t really notice that I was in WebPlayer mode since it is the default platform (for me), and I tend to code a bunch before I worry about what platform to build to. It’s also one of those things you don’t really think about being platform specific until someone mentions it.
To be clear, the solution for those having the problem will likely be:
File > Build Settings > Click on any other platform (i.e. Standalone PC/Mac) > Click “Switch Platform”
OMG Build settings was it for me too! I never changed it to Web Player, but since I work via Dropbox on 3 separate machines something got flipped during syncing.
I got the same problem and the same solution (changing target platform) worked. I’m on Unity 5.3.6f1. I really hope this is resolved in the next version and that somebody confirms/clarifies this.
I think that the webplayer is not allowed to call “ReadAllBytes” because the webplayer is sandboxed and read bytes is direct file access. This is just off the top of my head, it has been 5 years since I looked at this so I may be wrong.