0

I use unity3d and vs2019 to read a text file (txt) as follows:

    public class MeshTest01 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        string filePath = @"‪‪‪D:\Desktop\Unity\Mesh\77.txt";
        string[] lines = File.ReadAllLines(filePath);
        foreach (var item in lines)
        {
            Debug.Log(item);
        }
        //List<string> planesStr = TxtOperation.GetFaces2str(linesStr);
        //Debug.Log(TxtOperation.GetFaceIndex(planesStr[0]));

    }
}

The error information is as follows:

DirectoryNotFoundException: Could not find a part of the path "E:\project\GitHub\RobotSimulation\***D:\Desktop\Unity\Mesh\77.txt".

Why does the previous part of the file path appear

风何之
  • 43
  • 3
  • Did you paste the value of `filePath` from a document with mixed text direction? At the beginning of the string literal, after `@"` and before `D:`, you have the bytes `e2 80 aa` — the (unprintable) "Left-to-Right Embedding" character — repeated three times. As a result, `filePath` starts with something other than a drive letter so `ReadAllLines()` treats it as a relative path. To fix this, delete and retype the beginning of the string literal. This same type of problem happened recently in [C# error in reading file from another hard drive](https://stackoverflow.com/q/62704900/150605). – Lance U. Matthews Jul 14 '20 at 07:42
  • Does this answer your question? [Read text file return 'System.NotSupportedException' exception](https://stackoverflow.com/questions/39278233/read-text-file-return-system-notsupportedexception-exception) – Lance U. Matthews Jul 14 '20 at 07:45

1 Answers1

0

Honestly, I can't explain why this happens.

Usually yes, this would be one way of how to read an external file.

My guess would be that since your file is placed on a different drive the project might be sandboxed and therefore interprets the path as a relative one. Never tried to load files from different drives to be honest.


However, in general I would keep the files together with the project they belong to and place them

Then you would get your path using

var path = Path.Combine(Application.streamingAssetsPath, "SomeFolder", "77.txt");

or accordingly

var path = Path.Combine(Application.persistentDataPath, "SomeFolder", "77.txt");

For the streaming assets there are some special cases (e.g. on Android) where you need to read the file using UnityWebRequest.Get since it gets compressed.


Alternatively if it is an option for you you could also directly drag it into the Assets (or any folder below except special ones like StreamingAssets, Resources etc) and then directly drag it into a field via the Inspector using it as a (read-only) TextAsset

[SerializeField] private TextAsset meshFile;

and later access it's content via

var lines = meshFile.text.Split('/n');

Also I general: You should make a huge circle around the Resources!

Unity themselves strongly recommend to not use it! This and the reasons can be found in the Best practices.

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Thank you. Here is the code I wrote according to your suggestion![](https://gitee.com/fenghezhi/Pictures/raw/master/img/20200714151150.png) – 风何之 Jul 14 '20 at 07:16
  • @风何之 This would require that you put the file in `Assets/StreamingAssets/TextMesh/77.txt` – derHugo Jul 14 '20 at 07:23