Never Stop Learning
Never Stop Learning

Reputation: 785

Get Folder Path

I want to get the path of the folder to a textbox and the name of the folder is Images12345. I tried this.

//Inside the folder "Images12345"
string[] pics = { "1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg" };
int i = 0;

Then in my form load

//I tried this but it is given me wrong path
textBox1.Text = Path.GetFullPath(@"Images12345");
//then slideshow
pictureBox1.Image = Image.FromFile(textBox1.Text+"/"+pics[0]);
timer1.Enabled = true;
timer1.Interval = 5000;
i = 0;

Upvotes: 0

Views: 182

Answers (2)

cvsguimaraes
cvsguimaraes

Reputation: 13260

First, use this to get the application folder plus your images folder:

string applicationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images12345");

Second, always use Path.Combine to work with paths:

pictureBox1.Image = Image.FromFile(Path.Combine(myFolderPath, pics[0]));

NOTE

You'll need to copy images folder where the executable is. That's your bin/Degub while you're debugging. You can navigate two folders up, but you must to implement this like you were in production, when the executable will be right next to your image folder.


EDIT

Maybe it's a better approach to use the current user's pictures folder. Like this:

string userPicturesFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
string imagesFolder = Path.Combine(userPicturesFolderPath, "Images12345");

Upvotes: 2

John Gardner
John Gardner

Reputation: 25146

You already found Path.GetFullPath... you should also look at Path.Combine to make a path out of multiple pieces, instead of just using string concatenation.

Upvotes: 2

Related Questions