Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

`System.IO.File' does not contain a definition for `CreateText'

Discussion in 'Scripting' started by Preetthefire, Aug 7, 2012.

  1. Preetthefire

    Preetthefire

    Joined:
    Apr 12, 2012
    Posts:
    83
    Good morning every body
    I have this source:
    Code (csharp):
    1. if(hashTableChanged)
    2.             {
    3.                 Serialize();
    4.                
    5.                 StreamWriter fileWriter = null;
    6.                 fileWriter = File.CreateText(fileName);
    7.            
    8.                 if (fileWriter == null)
    9.                 {
    10.                     Debug.LogWarning("PlayerPrefs::Flush() opening file for writing failed: " + fileName);
    11.                 }
    12.                
    13.                 fileWriter.WriteLine(serializedOutput);
    14.                
    15.                 fileWriter.Close();
    16.  
    17.                 serializedOutput = "";
    18.             }
    But i don't know why i found this error:
    Any one has suggestions?
    Thanks have a nice day!!
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    switch the .net profile to the full 2.0 not subset.
    for some reason the utility functions in File are gone in the subset, this also includes WriteAllText / WriteAllBytes and their read counterparts
     
    TGAlencon likes this.
  3. Preetthefire

    Preetthefire

    Joined:
    Apr 12, 2012
    Posts:
    83
    Thanks for your response
    So what are the changes to apply to the source?
    Anything to change here?
    Thanks See you!
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    No changes to the source.
    The profile change has to be done in the player settings in Unity
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Actually it's probably because you have the webplayer selected. Webplayers don't have local file access.

    --Eric
     
    aditya007, Mondkin and Serhii-Horun like this.
  6. MariuszKowalczyk

    MariuszKowalczyk

    Joined:
    Nov 29, 2011
    Posts:
    301
    I have had similar problem: BCE0019: 'CreateText' is not a member of 'System.IO.File'.

    The solution for me was to remove #pragma strict from the script.
     
  7. JohnyDee

    JohnyDee

    Joined:
    Oct 3, 2012
    Posts:
    29
    Sorry, but "What is the function of this #pragma strict"?
     
  8. MariuszKowalczyk

    MariuszKowalczyk

    Joined:
    Nov 29, 2011
    Posts:
    301
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That would have no effect. Don't remove #pragma strict.

    --Eric
     
  10. Lina Ku

    Lina Ku

    Joined:
    Nov 12, 2012
    Posts:
    5
    Where can I switch this .net profile, I am using mac, I am getting similar error while compiling. Following is my code snippet

    string dataPath = Application.persistentDataPath + "/scores.dat";
    StreamWriter stream = File.CreateText( dataPath );
    if(stream)
    {
    string xml = SerializeObject( this );
    stream.Write( xml );
    }
     
  11. NewDeveloper

    NewDeveloper

    Joined:
    Aug 25, 2012
    Posts:
    153
    I am not sure, because I never used something like this, but I suggest you to check if did you imported the IO library.
     
  12. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    In the player settings.

    --Eric
     
  13. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    In the menu bar hit 'edit'>'Project Settings'>'Player'

    Then in the inspector hit 'other settings' and near the bottom of the inspector will be an 'Api Compatibility Level'.
     
  14. Lina Ku

    Lina Ku

    Joined:
    Nov 12, 2012
    Posts:
    5
    Thanks for your quick reply.

    I have tried to link with both the settings (with .Net 2.0 and .NET 2.0 Subset) the problem still persists.

    I have imported IO library, I am getting this error only for File.CreateText method where as File.OpenText and other methods links fine.
     
    Last edited: Jan 18, 2013
  15. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Make sure you are using a build platform that supports writing to player prefs (such as desktop iOS or android) - as Eric said.

    ~A
     
  16. #VRLAB's POLAND

    #VRLAB's POLAND

    Joined:
    May 9, 2013
    Posts:
    1
    Hey All,

    I got a solution for you.

    Simply use platform based comments, whenever you have to use code that you know will run only on specified platform use:

    string Settings;

    #if UNITY_STANDALONE

    // your code e.g

    Settings = File.ReadAllText(Application.dataPath+"/GlobalSettings.txt");

    #elif UNITY_WEBPLAYER

    Settings = Application.ExternalCall("GetSettings"); // this actualy won't work.. but you get the idea ;)

    #endif

    :cool:
     
    Last edited: May 9, 2013
  17. anshikagarg

    anshikagarg

    Joined:
    Apr 17, 2014
    Posts:
    1
    Hi Eric,

    I wanted to make a web application. Could you suggest a way I could get rid of this problem?

    Thanks
    Anshika
     
  18. meat5000

    meat5000

    Joined:
    Mar 5, 2013
    Posts:
    118
    Local file access is restricted from Webplayer to address Glaring security issues.