[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Copy files into the executable directory in C#

[Copy files into the executable directory in C#]

Sometimes it's handy to copy a file into the program's executable directory. For example, it's sometimes nice to copy a configuration file, database file, or other data file into the directory where the executable program is created. That way the executable program knows it can find the file.

To copy a file into the executable directory, first add it to the project. To do that, open the Project menu, select Add Existing Item and select the file.

Next click on the file in Solution Explorer. In the Properties window, click on the "Copy to Output Directory" property and select one of the following options.

  • Do not copy - Don't copy the file into the executable directory
  • Copy always - Copy the file into the executable directory every time Visual Studio builds the executable
  • Copy if newer - Copy the file into the executable directory if it is newer than the copy that is already in that directory. (I.e. it has been modified since it was last copied).

Now the program can assume the file is in the same directory where it is executing. The following code shows how the example program displays an image file on its background assuming the file is in the program's startup directory.

// Load the picture from the startup directory. private void Form1_Load(object sender, EventArgs e) { BackgroundImage = new Bitmap("essential_algs_m.jpg"); ClientSize = BackgroundImage.Size; }

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.