Search Unity

Reading external TXT file with streamreader gives ans IO ERROR

Discussion in 'Editor & General Support' started by kordou, Mar 24, 2018.

  1. kordou

    kordou

    Joined:
    May 16, 2017
    Posts:
    13
    Hi,
    I am strangling to find an error to my reading txt file code because i getting this error. Could soumeone help please!!


    code:

    public void ReadServerFile(string nameOfFile) {

    string file = System.IO.Path.Combine (MyAppsDataPath, System.IO.Path.GetFileName (nameOfFile));

    try{

    using (StreamReader SR = new StreamReader (file))
    {
    string line;
    while (!string.IsNullOrEmpty (line = SR.ReadLine ())) {
    line.Trim();
    string[] parts = line.Split (new char[] { ',' });
    TargetInfo objTargetInfo = new TargetInfo ();
    objTargetInfo.texta = parts [0];
    objTargetInfo.textb= parts [1];
    objTargetInfo.textc= parts [2];
    lines.Add (objTargetInfo);
    }
    SR.Close ();
    }
    }

    catch(IOException e){
    Debug.Log (e);
    }
    }


    error
    System.IO.IOException: Sharing violation on path C:\Users\koukos\AppData\LocalLow\yourwed_gr\testcodes\mtf\eanos.txt
    at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00251] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/FileStream.cs:320
    at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) [0x00000] in <filename unknown>:0
    at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
    at System.IO.File.OpenRead (System.String path) [0x00000] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:363
    at System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) [0x00077] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/StreamReader.cs:167
    at System.IO.StreamReader..ctor (System.String path) [0x00000] in <filename unknown>:0
    at (wrapper remoting-invoke-with-check) System.IO.StreamReader:.ctor (string)
    at DataHandler.ReadServerFile (System.String nameOfFile) [0x00015] in G:\ANDROID\unity projcts\testcodes\Assets\MyScripts\Data\DataHandler.cs:83
    UnityEngine.Debug:Log(Object)
    DataHandler:ReadServerFile(String) (at Assets/MyScripts/Data/DataHandler.cs:100)
    DataHandler:Start() (at Assets/MyScripts/Data/DataHandler.cs:39)