Click here to Skip to main content
15,910,130 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have to find the file then click open every time i want the file to show up in the form.

What I have tried:

i used the open file dialog option but dont want to have to find that file every i want to open it.
Posted
Updated 16-May-16 15:34pm
Comments
Richard MacCutchan 16-May-16 14:52pm    
Then you need to hard code its location somewhere so the application knows where it is.
Ashton Lewis 16-May-16 15:10pm    
can you please help me out with the code to do that. i'm relative new to programming.
Sergey Alexandrovich Kryukov 16-May-16 16:47pm    
There is no such thing as "default pdf file".
—SA
George Jonsson 16-May-16 21:43pm    
The OP probably means "open a file with a default path and name"
Sergey Alexandrovich Kryukov 16-May-16 21:45pm    
This is almost the same; there is no such thing.
—SA

1 solution

Hard coding a path is rarely the best of ideas.
What you can do is that you use the file dialog the first time, and then you save the path in the application settings.
Then your app can check if a stored path exists and that it is a valid path.
If not, you open the file dialog again.

See Application Settings Overview[^]
and
Using Settings in C#[^]

The code could be something like this:
C#
if (string.IsNullOrEmpty(Properties.Settings.Default.MyStoredFilePath) 
    || !File.Exist(Properties.Settings.Default.MyStoredFilePath))
{
    // Open the file dialog and set the path
    if (fileDialog.ShowDialog() == DialogResults.OK)
        Properties.Settings.Default.MyStoredFilePath = fileDialog.FileName;
}

// Open your document using the path variable
if (File.Exist(Properties.Settings.Default.MyStoredFilePath))
    OpenDocumentOrWhatEverYourMethodIs(Properties.Settings.Default.MyStoredFilePath);
else
    // What ever you need to do
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900