Izze
Izze

Reputation: 13

How do I solve the System.IO.FileNotFoundException?

I'm trying to load a picture in MonoGame (without Content.Load), But when I try to launch the project I encounter the System.IO.FileNotFoundException and I was hoping someone could tell me what I'm doing wrong.

Error message: System.IO.FileNotFoundException: 'Could not find file 'G:\Min enhet\C#\FirstMonoGameProject\bin\Debug\netcoreapp3.1\Content\Graphic\CoolBox'.'

The code where I get the error

 public Texture2D LoadTexture()
        {

   
            string fullPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
            + "/Content/Graphic/" + "CoolBox";


            using (FileStream fileStream = File.Open(fullPath, FileMode.Open))
            {

                Texture2D myTexture2D = Texture2D.FromStream(EntityManager.graphicsDevice, fileStream);
                return myTexture2D;

            }

        }

Additional code where I call on the method (both from game1)

 protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
           
            coolBox.LoadTexture();

        }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();

        spriteBatch.Draw(coolBox.LoadTexture(), new Rectangle(0, 0, 280, 210), Color.White);

        spriteBatch.End();

        base.Draw(gameTime);
    }

Upvotes: -3

Views: 34274

Answers (4)

veysel_alan
veysel_alan

Reputation: 1

First of all, I get this error in .Net 8.0. In order to correct the error, the file to be opened should be thrown into the debug folder in the bin folder in the project folder. Afterwards, if it is thrown into net8.0 and the project is run, healthy results are obtained.

Upvotes: 0

Florent
Florent

Reputation: 137

Also check your actual file name. When you go to the properties of the file and the name is called lets say test.txt then the actual file name is test.txt.txt. When creating a file through the explorer for example write always the name without the extension. Just if it helps someone else

Upvotes: -1

hesolar
hesolar

Reputation: 709

System.IO.FileNotFoundException appears when the path that you are using is wrong , it means that the file doesnt exist. Your problem cames on the variable string fullpath wich has a wrong path.

First check on your computer(windows search : windows+s) that the file exists by copying there the full path. Anyway to prevent this exception you can use try catch block to display a message if the file dont exists. Other solution can be using File.Exists(path) , this way you dont throw an exception.

Upvotes: 1

Piotr Golacki
Piotr Golacki

Reputation: 206

First I would add checking for the file existence using File.Exists(String) method. Also I found helpful is to use Path.Combine() method to properly create a path instead of manually concatenating strings.

Upvotes: 1

Related Questions