0

I am making an application called Profiler to store some people's profiles, and will use a database to store the profile photo's path.

This is my FindPhoto button (to upload the profile photo):

private void btnFindPhoto_Click(object sender, EventArgs e)
{
    ofdFindPhoto.Title = "Select a Photo ";
    ofdFindPhoto.InitialDirectory = @"D:\Desktop";
    ofdFindPhoto.FileName = "";
    ofdFindPhoto.Filter = "JPEG Image|*.jpg|GIF Image|*.gif|PNG Image|*.png|BMP Image|*.bmp";
    ofdFindPhoto.Multiselect = false;
    if (ofdFindPhoto.ShowDialog() != DialogResult.OK) { return; }

    // Show the recently uploaded photo on profile
    picProfilePhoto.ImageLocation = ofdFindPhoto.FileName;
}

When the user clicks the button to save the profile, I get the txtName.Text, txtPhone.Text, txtDescription.Text and photo path to save to the database. I want to use something like this to save the photo path:

string photoPath = Application.StartupPath + @"\Photos\" + txtName.Text +
                   Path.GetExtension(ofdFindPhoto.FileName);

This should generate some path like:

Drive:\InstalledPath\Profiler\Username.jpg (or whatever extension)

In short, I need to place a folder inside the application's root, but this folder should be created on install or first run (preferably that I can use on debug, too).

2 Answers 2

2
    if(!Directory.Exists(Application.StartupPath + @"\Photos"))
         Directory.CreateDirectory(Application.StartupPath + @"\Photos");
Sign up to request clarification or add additional context in comments.

Comments

1

try this in program.cs.It checks for folder when the application in opened

          if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "Profiles"))
            {
                Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "Profiles");

            }
     Application.Run(new form1());

2 Comments

The slashes in your file paths should be backslashes.
@FabianBigler thanks for pointing that.Works without slashes too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.