I've created an application which saves files to a specific directory. What I would like is to click a button, I can open this .pdf file.
I've created an application which saves files to a specific directory. What I would like is to click a button, I can open this .pdf file.
C # and WindowsForms
private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\" ;
openFileDialog1.Filter = "PDF files (*.pdf)|*.pdf" ;
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
System.Diagnostics.Process.Start(openFileDialog1.FileName);
}
}
The code above opens, when the button is clicked, a dialog where a pdf file can be selected.
The file will be opened by the application associated with the .pdf
extension.
When dragging a control of type OpenFileDialog, simply go to the file 'designer' and set the properties, according to the previous answer.
If you need to put more than one option in the 'Filter' property:
openFileDialog1.Filter = "Texto|*.txt;*.dat;*.csv|" + _
"imagem|*.jpg;*.jpeg;*.gif|" + _
"audio|*.mp3;*.aac;*.wma|" + _
"planilha Excel|*.xls|" + _
"Todos os arquivos|*.*"
Note: The 'Filter' property receives only one string. I just broke the line for easy reading.
'Declarepara Executar
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
'Sub para Executar
Sub subExecuta(strPath As String)
ShellExecute 0, "Open", strPath, "", "", vbNormalFocus
End Sub
'Executa
call subExecuta(App.Path & "\file.pdf")