Press "Enter" to skip to content

Lesson 19.3: Move data and image files out of Engine project

Currently, game data and image files are included in the Engine project. In this lesson, we’ll move them out.

They aren’t code, so it makes sense to get them out of a class library project. This will also help if we add more files. This technique always copies every file in the folders – giving us one less thing for us to remember (or forget).

We’re going to do this with a “post-build event”

Step 1: Create GameFiles folder

Create this new folder under your solution’s top-level directory

Step 2: Move files from Engine directory to GameFiles directory

Cut-and-paste GameData folder

Cut-and-paste Image folder

Step 3: Create post-build event to copy files

In the WPFUI project, right-click and select “Properties”.

On the properties screen, select the “Build Events” tab.

Copy this line into the “Post-build event command line box”. This command will run after each time we build the project/solution, copying all the files and folders in the GameFiles folder to the project’s output folder – the location where the executable version of the program is located.

xcopy “$(SolutionDir)GameFiles\*.*” “$(ProjectDir)$(OutDir)” /s /y

Step 4: Test the game

Rebuild the solution and make sure the games still runs correctly.

NEXT LESSON: Lesson 19.4: Replace BaseNotificationClass with Fody PropertyChanged Notification

PREVIOUS LESSON: Lesson 19.2: Identifying refactoring targets


  1. Phil Eakins
    Phil Eakins 2021-11-26

    So — MSDos is not dead!

      SOSCSRPG 2021-11-26

      MS-DOS is always there, waiting for us. πŸ™‚

  2. Julian
    Julian 2021-12-13

    Hi Scott, this change has broken my unit tests – not sure if it did for you.
    It appears to be because we have constants in our Factories using relative paths to our game data files.
    Since our executing directory for the unit tests is not the WPFUI bin location, the tests fail.
    I wasn’t sure of the best course of action to fix this. Simply changing directory in the unit tests could do it but seems hacky.

      SOSCSRPG 2021-12-13

      Hi Julian,

      I think I deleted the unit test project when we added the dynamic player attributes. You could try adding an XCOPY for the unit test project, but I plan to completely redo the unit tests some time after finishing the refactoring. With the new solution structure, and removal of the circular project dependencies, it should be much easier to create unit tests.

      If you do the XCOPY, please post a comment here, for anyone else who may want to run the original unit tests.

      • Julian Smith
        Julian Smith 2021-12-14

        I can confirm using the same xcopy in the post build of the unit tests project made them work.

  3. Julian
    Julian 2021-12-13

    Of course including the post build xcopy in the unit test project would also probably work.

  4. Ted Bunny
    Ted Bunny 2022-02-04

    (Visual Studio 2019) Is there a way to get these files to still appear in the Solution Explorer? “Add existing Item” doesn’t mimic the folder structure, and maintaining that by manually recreating the folders and adding the objects every time one is added seems tedious.

      SOSCSRPG 2022-02-04

      I don’t know a way to include those files in Solution Explorer.

      I’m not sure what requires manually recreating the folders and adding the objects” refers to. Are you talking about after each build? If so, adding the “xcopy” command to the post-build event (in step 3) should copy the game data and image files every time you build. Is that not happening, or are you referring to something else?

  5. Johan
    Johan 2022-04-30


    For those who get error code 4 while copying, it is due to spaces in the folder and/or filenames.

    To solve this you will need to close the destination and target with quotes, like this:

    xcopy “$(SolutionDir)GameFiles\*.*” “$(ProjectDir)$(OutDir)” /s /y

      SOSCSRPG 2022-05-02

      Hi Johan,

      Thanks for sharing that. I updated the lesson to use that format.

  6. Betsy
    Betsy 2023-05-30

    Hello, I left a comment earlier today about not having the post-build command running correctly, and I think I found the issue just now, having to do with the different “types” of quotation marks

    When I copied/pasted your xcopy code, it copied with the character ” (more curly quotations) which VS was not parsing correctly. Replacing all of them with ” (straight quotations) made the post-build run correctly. You don’t have to respond to this, but maybe check the formatting on this page so others don’t have the same issue.

    Thanks for everything you do! I’ve been really enjoying going through these lessons.

      SOSCSRPG 2023-05-31

      Thanks for letting me know. The source code formatting tool for this site broke a while back and I had to add a replace it. I thought I caught all the “smart quotes” and replaced them, but it looks like there are a few more. I’ll see if I can correct this lesson tomorrow.

  7. Alex Veres
    Alex Veres 2023-10-03


    For those who get error couldn’t copy files from Engine/Images because its not exists:

    Step1: Copy the Images and GameData folders, paste to your solution’s top-level “GameFiles” directory in your windows explorer.
    Step2: Delete the Engine/Images and Engine/GameData from your solution inside VisualStudio.
    Step3: Post-build event command: xcopy β€œ$(SolutionDir)GameFiles\*.*” β€œ$(ProjectDir)$(OutDir)” /s /y

    It should works properly now πŸ™‚

    In my Step2 I just deleted the Engine/Images and Engine/GameData folders in my windows explorer instead of Visual Studio’s Solution Explorer and When I wanted to build my solution I got some errors.

      SOSCSRPG 2024-05-26

      Hi Jonathan,

      It looks like the problem was that my page formatted the xcopy command with “smart quotes”, and the build had a problem with that. I’ve changed the quotes to standard quotes in the lesson, so you can re-copy it or just go into the project properties and replace them there.

      Please let me know if that doesn’t fix the error.

      • Jonathan Rodriguez
        Jonathan Rodriguez 2024-05-27

        Still no working I dont know a I take the right one xcopy.

        • SOSCSRPG
          SOSCSRPG 2024-05-27

          I pushed the change to your GitHub repository. Can you try pulling it down and see if that works?

          • Jonathan Rodriguez
            Jonathan Rodriguez 2024-05-28

            It works ! Thank you so much !

Leave a Reply

Your email address will not be published. Required fields are marked *