directories and application path

B

Bart

Hi all,

I would like to open an OpenFileDialog to find an image and store it in the
folder of my application, so I can refere to it in the future only by
"./picture/file".
My code looks like that:

//openFileDialog and set up Filter,Title and Initial Directory
openFileDialog1.Filter = "Bitmap Image(*.bmp)|*.bmp|GIF Image
(*.gif)|*.gif|JPEG Image(*.jpg)|*.jpg|All files (*.*)|*.*" ;
openFileDialog1.Title = "Choose a picture";
openFileDialog1.InitialDirectory = "C:\\";

if (openFileDialog1.ShowDialog()== DialogResult.OK)
{
pictureBox1.Image=Image.FromFile(openFileDialog1.FileName);
System.IO.FileInfo MyFile=new
System.IO.FileInfo(openFileDialog1.FileName);
MyFile.CopyTo(@".\Picture\"+MyFile.Name,true);
PicturePath=".\\Picture\\"+MyFile.Name;
}

now the problem is that if I do like that it will create a folder "Picure"
in each directory I have found the image! How could I set up the directory
of my application?
Thank you very much,

Bart
 
G

Guest

Hi Bart,

Is this what you you looking for: Application.StartupPath?

HTH,
Rakesh Rajan
 
A

Arne Schittenhelm

Hello Bart,

Try this:

openFileDialog1.InitialDirectory =
Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
openFileDialog1.Multiselect = true;

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string dest = Application.StartupPath + @"\Picture";
if (Directory.Exists(dest) == false)
Directory.CreateDirectory(dest);

foreach (string picture in openFileDialog1.FileNames)
{
string filename = picture.Remove(0, picture.LastIndexOf(@"\"));
File.Copy(picture, dest+filename);
}
}

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top