-2

Possible Duplicate:
Getting the application's directory from a WPF application

I want to acess files from project directory like in java without using "C:\Path" because it create file exception into my picture box this is code into my timer

if (imagecount == 30)
{
    this.pictureBox1.Image = System.Drawing.Image.FromFile(@"C:\Users\Baloi\Documents\visual studio 2010\Projects\WinEX\WinEX\" + image() + ".jpg");
    imagecount = 0;
}

else if (imagecount < 30)
    imagecount++;
Community
  • 1
  • 1
  • Also similar to: http://stackoverflow.com/questions/3259583/how-to-get-files-in-a-relative-path-in-c-sharp – Cᴏʀʏ Jun 03 '12 at 18:21

3 Answers3

2

Aplication directory:

string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;

Executable Directory:

string executableDirectory = Path.GetDirectoryName(Application.ExecutablePath);

Based on your requirement you can use one of above with Path.Combine and build full path to your image location.

Or you can embed images in a resource file. then you can load them as

Stream imgStream = 
    Assembly.GetExecutingAssembly().GetManifestResourceStream(
    "YourNamespace.resources.ImageName.bmp");
pictureBox.Image = new Bitmap(imgStream);
0

You can use the code:

this.pictureBox1.Image = System.Drawing.Image.FromFile(image() + ".jpg");

Your file should be in the same folder as the program.

0

You have a few options:

  1. Embed the pictures in your project (set the compile action to Embedded Data)

  2. Reference your pictures using a relative path. This is complicated slightly by the fact that, while debugging, the binary assemblies are in the bin\Debug folder

For option 1:

System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file = 
    thisExe.GetManifestResourceStream("AssemblyName.ImageFile.jpg");
this.pictureBox1.Image = Image.FromStream(file);

http://msdn.microsoft.com/en-us/library/aa287676(v=vs.71).aspx

For option 2:

string appPath = Path.GetDirectoryName(Application.ExecutablePath);
if (System.Diagnostics.Debugger.IsAttached)
{
    contentDirectory = Path.Combine(appPath + @"\..\..\content");
}
else
{   
    contentDirectory = Path.Combine(appPath, @"content");
}
Eric J.
  • 147,927
  • 63
  • 340
  • 553