2
votes

I have a program written in Unity using C# that initializes a new StreamReader and proceeds to read the text data from a text file I have stored in Unity's resources folder. Everything works fine when I click play in Unity -everything works and the text is read and displayed perfectly. However, when I try to build it in order to run it via the HoloLens emulator (Platform: Windows Store, SDK: Universal 10, Build and Run On: Local Machine), I get the error: error CS1503: Argument 1: cannot convert from 'string' to 'System.IO.Stream'.

I don't understand why this error is even showing up in the first place as the constructor for a StreamReader has an overload that accepts a string parameter.

My code is as follows:

string metadata = String.Format("/Resources/.../metadata.txt", list);
if (File.Exists(Application.dataPath + metadata))
{
     using (StreamReader sr = new StreamReader(Application.dataPath + metadata))
            {
                  // ....
            }
}
2
I can only suppose that there is no overload of StreamReader that takes the filename as parameter but only one that takes just a stream - Steve
There is a string overload in the normal System.IO.StreamReader, is that the one you are using? - XerShade
I believe I am using the string overload in the normal System.IO.StreamReader as Application.dataPath returns a string and metadata is a string. "using System.IO" is required for this to run correctly. Everything compiles and runs fine when clicking play button through Unity; however, when trying to build it as a Windows app, I am getting compilation errors stated in the question. - whycodingsohard
When you build in Unity, you are given a new project. Then you double click the solution which opens a new Visual Studio. There, you need to go to the error (that error which did not show earlier). Remove the error and start typing again the same. It will start autocompletion and intellisense, that will tell you whether you can use the StremReader(string) or not. - Everts

2 Answers

4
votes

I agree with the others, this is likely caused by a diffence between mono in the editor and the .net that you are compiling with to get a UWP application. Try this instead:

using(StreamReader sr = new StreamReader(new FileStream(Application.dataPath + metadata, FileMode.Open)))

This should be legal mono and .net code.

1
votes

The API differs in some cases between Unity Mono and .NET on UWP. It could be the StremReader(string) ctor is missing from the UWP version.

For instance, I had a case where Delegate.CreateInstance works in Editor but fails on Hololens and requires a different version.

You can wrap things in macros or use the one UWP requires.