Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7031989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:52:32+00:00 2026-05-28T00:52:32+00:00

My program has a button which when clicked opens an openfiledialog to choose a

  • 0

My program has a button which when clicked opens an openfiledialog to choose a picture:

private string ChoosePicture()
{         
    fDialog.Title = "Select Picture";
    fDialog.Filter = "Image Files (*.bmp, *.gif, *.jpg)|*.bmp; *.gif*;*.jpg";
    fDialog.InitialDirectory = "C:";
    fDialog.ShowDialog();

    fDialog.AddExtension = true;
    fDialog.CheckFileExists = true;
    fDialog.CheckPathExists = true;

    //returns a string for the directory
    return fDialog.FileName.ToString();
}

Using a check on the dialogresult box hasn’t resolved my issue either:

fDialog.AddExtension = true;
fDialog.CheckFileExists = true;
fDialog.CheckPathExists = true;

DialogResult res = fDialog.ShowDialog();
if (res == DialogResult.OK)
{                
    //returns a string for the directory
    return fDialog.FileName.ToString();
}

return null; 

The code works if I do choose a picture and complete the file selection. However if I cancel the process at any point in between I get the exception “The path is not of a legal form”. I am not sure which part I imagine I could take care of this with a try-catch, however I’m not positive which part is causing the issue? If I put a try catch around the call to the ChoosePicture() method, I can at least stop it from crashing the program but the exception is still being thrown when no picture is selected in the fdialogbox.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:52:33+00:00Added an answer on May 28, 2026 at 12:52 am
    DialogResult result = fileDialog.ShowDialog();
    if (result == DialogResult.OK) {
    
         //returns a string for the directory
         return fDialog.FileName;
    }
    
    return null; //not sure what you will return if they cancel
    

    also, FileName is already a string, so no need to use .ToString() on it

    EDIT: fixed indenting

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an android program. It has a button which when clicked is supposed
I've made a small program which has 2 buttons and each does certain thing.
My program has the following class definition: public sealed class Subscriber { private subscription;
When i click on done button which has code UIBarButtonItem * button = [[[UIBarButtonItem
My program has 3 text fields, Title, Website, and PictureURL. When I click the
Like most laptops, mine (a Dell Inspiron 1420) has a small button which can
I wrote a simple Android program with 2 tabs. Tab 1 has a button
I wrote a simple Android program with 2 tabs. Tab 1 has a button
I've created a simple ribbon button which opens a new ASPX page and passes
I have a form that has a button which creates a new object and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.