error: `System.IO.File' does not contain a definition for `ReadAllBytes'

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?

Cheers,
Grant

Works fine here.

–Eric

Thanks.

Anyone have any suggestions? I have

  • redownloaded 3.4
  • re installed 3.4
    -set API compatibility to .NET 2.0
    -disabled stripping.
    -rebooted :slight_smile:

I am having the problem with easysave code and have written them. Although it sounds like it is a problem with my install not their code.

Cheers,
Grant

Did you try adding

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

It should work with .NET 2.0 subset. I doubt it’s your install, or I think nothing would work at all.

–Eric

the top of script has

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).

Cheers,
Grant

FYI:My project has recorrupted itself :frowning:
I will send a bug report in.

I’m seeing this behavior too. I upgraded to Unity 3.4 and this error appeared shortly after (but not immediately after) the upgrade.

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 :frowning:

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
    }
}

Edit: New and improved:

thanks NPSF3000

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.

I saw this behavior outside the webplayer, Android build to be exact, where it was previously working.

old tread, but my solution was in build settings switch from web to windows platform

2 Likes

If you are using web platform, you cannot use System.IO.File, change the build settings to Windows.

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.

There’s nothing to resolve? ReadAllBytes, and indeed much direct file access, isn’t allowed in browser and will never work when targeting web.