Qapples
Qapples

Reputation: 23

Monogame - "The content file was not found" exception despite the content file being the location specified by the exception

I'm having an unusaul problem when attempting to load .png files in monogame.

Despite the file being in the same exact location, I'm getting an exception saying:

Unhandled Exception: Microsoft.Xna.Framework.Content.ContentLoadException: The content file was not found. ---> System.IO.FileNotFoundException: Could not find file 'C:\Users\...\RiderProjects\PointAndClickEngine\PointAndClickEngine\Content\EnabledButton
.xnb'.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Bo
olean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   at Microsoft.Xna.Framework.Content.ContentManager.OpenStream(String assetName)
   --- End of inner exception stack trace ---
   at Microsoft.Xna.Framework.Content.ContentManager.OpenStream(String assetName)
   at Microsoft.Xna.Framework.Content.ContentManager.ReadAsset[T](String assetName, Action`1 recordDisposableObject)
   at Microsoft.Xna.Framework.Content.ContentManager.Load[T](String assetName)
   at PointAndClickEngine.Game1.LoadContent() in C:\Users\...\RiderProjects\PointAndClickEngine\PointAndClickEngine\Game.cs:line 74
   at Microsoft.Xna.Framework.Game.Initialize()
   at PointAndClickEngine.Game1.Initialize() in C:\Users\...\RiderProjects\PointAndClickEngine\PointAndClickEngine\Game.cs:line 60
   at Microsoft.Xna.Framework.Game.DoInitialize()
   at Microsoft.Xna.Framework.Game.Run(GameRunBehavior runBehavior)
   at Microsoft.Xna.Framework.Game.Run()
   at PointAndClickEngine.Program.Main() in C:\Users\...\RiderProjects\PointAndClickEngine\PointAndClickEngine\Program.cs:line 11

Here is an image of my directory: Image

Content Folder:enter image description here

Contents of Content.mgcb:

#----------------------------- Global Properties ----------------------------#

/outputDir:bin
/intermediateDir:obj
/platform:Windows
/config:
/profile:Reach
/compress:False

#-------------------------------- References --------------------------------#


#---------------------------------- Content ---------------------------------#

#begin ButtonDisabled.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:ButtonDisabled.png

#begin ButtonEnabled.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:ButtonEnabled.png

#begin Door1.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:Door1.png

#begin Door2.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:Door2.png

#begin Door3.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:Door3.png

#begin Door4.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:Door4.png

#begin Door5.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:Door5.png

#begin Door6.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:Door6.png

#begin TestBackground.png
/importer:TextureImporter
/processor:TextureProcessor
/processorParam:ColorKeyColor=255,0,255,255
/processorParam:ColorKeyEnabled=True
/processorParam:GenerateMipmaps=False
/processorParam:PremultiplyAlpha=True
/processorParam:ResizeToPowerOfTwo=False
/processorParam:MakeSquare=False
/processorParam:TextureFormat=Color
/build:TestBackground.png

What's even more strange is that TestBackground.png g ets loaded succesfully in for some reason. I have absolutely no clue what could be casuing this. I've tried moving the files around to see if I could get it working but to no avail. After looking online for a bit, there seems to be no one else having this problem. Game.cs:

using System;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace PointAndClickEngine
{
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        //When the mouse reaches the rectangle, the camera pans
        Rectangle leftMoveBoundary;
        Rectangle rightMoveBoundary;

        //Border sprites for testing
        Texture2D leftBoundarySprite;
        Texture2D rightBoundarySprite;

        Background background;
        Element[] elements;

        int width;
        int height;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            //temporry file location
            Content.RootDirectory = @"FILELOCATION";
        }

        protected override void Initialize()
        {
            width = 1600;
            height = 900;

            graphics.PreferredBackBufferWidth = width;
            graphics.PreferredBackBufferHeight = height;

            //Boundary texture. USED FOR TESTING
            leftMoveBoundary = new Rectangle(0, 0, graphics.PreferredBackBufferWidth / 4,
                graphics.PreferredBackBufferHeight);
            rightMoveBoundary = new Rectangle((int) (graphics.PreferredBackBufferWidth * 0.75), 0,
                graphics.PreferredBackBufferWidth / 4, graphics.PreferredBackBufferHeight);

            leftBoundarySprite = new Texture2D(GraphicsDevice, leftMoveBoundary.Width, leftMoveBoundary.Height);
            leftBoundarySprite.SetData<Color>(
                new Color[leftBoundarySprite.Width * leftBoundarySprite.Height].Select(e => Color.Blue).ToArray());

            rightBoundarySprite = new Texture2D(GraphicsDevice, rightMoveBoundary.Width, rightMoveBoundary.Height);
            rightBoundarySprite.SetData<Color>(
                new Color[rightBoundarySprite.Width * rightBoundarySprite.Height].Select(e => Color.Green).ToArray());

            graphics.ApplyChanges();

            base.Initialize();
        }

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

            //Load door textures
            Texture2D[] doorTextures = new Texture2D[6];
            for (int i = 0; i < 6; i++)
            {
                doorTextures[i] = Content.Load<Texture2D>("Door" + i + 1);
            }

            elements = new Element[]
            {
                new Door(doorTextures, Vector2.Zero, 10),
                new Door(doorTextures, Vector2.Zero, 10),
                new DoorButton(Content.Load<Texture2D>("EnabledButton"), Content.Load<Texture2D>("DisabledButton"),
                    elements[0] as Door, new Rectangle(0, (int) (height * 1.25), 158, 128)),
                new DoorButton(Content.Load<Texture2D>("EnabledButton"), Content.Load<Texture2D>("DisabledButton"),
                    elements[1] as Door, new Rectangle((int) (width / 1.1), (int) (height * 1.25), 158, 128)),
            };
            //When I delete every content load above this, the program works and no execption is thrown
            background = new Background(Content.Load<Texture2D>("TestBackground"), elements);
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            InputHelper.PreviousMouseState = Mouse.GetState();


            foreach (Element e in elements)
            {
                if (e is Door door)
                {
                    door.Update(gameTime);
                }
                else
                {
                    e.Update();
                }
            }

            base.Update(gameTime);
        }

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

            spriteBatch.Begin();

            spriteBatch.Draw(leftBoundarySprite, leftMoveBoundary.Location.ToVector2(), Color.Blue);
            spriteBatch.Draw(rightBoundarySprite, rightMoveBoundary.Location.ToVector2(), Color.Green);

            spriteBatch.Draw(background.Texture, new Vector2(0, 0));

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

Any help is appreciated!

Upvotes: 0

Views: 1758

Answers (1)

Jonathan Alfaro
Jonathan Alfaro

Reputation: 4376

The problem you are having is that it is trying to load a file that does not exist.

The exception says it is looking for file "EnabledButton.xnb" but the folder you showed the file is named "ButtonEnabled.xnb". So the name is not the same.

I imagine the same situation will occur for "DisabledButton.xnb".

So these lines of code:

 elements = new Element[]
        {
            new Door(doorTextures, Vector2.Zero, 10),
            new Door(doorTextures, Vector2.Zero, 10),
            new DoorButton(Content.Load<Texture2D>("EnabledButton"), Content.Load<Texture2D>("DisabledButton"),
                elements[0] as Door, new Rectangle(0, (int) (height * 1.25), 158, 128)),
            new DoorButton(Content.Load<Texture2D>("EnabledButton"), Content.Load<Texture2D>("DisabledButton"),
                elements[1] as Door, new Rectangle((int) (width / 1.1), (int) (height * 1.25), 158, 128)),
        };

Should read like this:

 elements = new Element[]
        {
            new Door(doorTextures, Vector2.Zero, 10),
            new Door(doorTextures, Vector2.Zero, 10),
            new DoorButton(Content.Load<Texture2D>("ButtonEnabled"), Content.Load<Texture2D>("ButtonDisabled"),
                elements[0] as Door, new Rectangle(0, (int) (height * 1.25), 158, 128)),
            new DoorButton(Content.Load<Texture2D>("ButtonEnabled"), Content.Load<Texture2D>("ButtonDisabled"),
                elements[1] as Door, new Rectangle((int) (width / 1.1), (int) (height * 1.25), 158, 128)),
        };

Notice how I changed EnabledButton for ButtonEnabled and DisabledButton for ButtonDisabled.

Upvotes: 2

Related Questions