本文介绍了如何使openfiledialog.initialdirectory打开整个路径而不仅仅是第一级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击一个按钮时,我试图让我的程序打开一个文件夹PHRF List,使用OpenFileDialog.InitialDirectory =C:\ Race \ PHRF List \。该代码将我带到C:\ Race \,但不是下一级文件夹PHRF列表。 InitialDirectory只打开C下面的第一个级别吗?以下是此Sub的完整代码:



When I click a button I am trying to have my program open a folder, "PHRF List", using OpenFileDialog.InitialDirectory = "C:\Race\PHRF List\". The code takes me to "C:\Race\" but not to the next level folder PHRF List. Does InitialDirectory open only the first level below C:? Here is the complete code for this Sub:

Private Sub btnLoadList_Click(sender As Object, e As EventArgs) Handles btnLoadList.Click
       If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
           OpenFileDialog1.InitialDirectory = "C:\Race\PHRF List\"
           OpenFileDialog1.Filter = "txt files(*.txt)|*.txt"                   '
           txtPHRFfile.Text = OpenFileDialog1.FileName
           OpenFileDialog1.RestoreDirectory = True
       End If
       Thread.Sleep(500) 'Enter a delay here
       Using readFile As New StreamReader(txtPHRFfile.Text)
           Do While readFile.Peek() <> -1
               blPHRFstr = readFile.ReadLine()
               listArr = Split(blPHRFstr, ",")
               Me.dgv21.Rows.Add(listArr)
           Loop
           readFile.Close()
           readFile.Dispose()
       End Using
   End Sub





我尝试了什么:



阅读许多在线文章,包括使用InitialDirectory的Microsoft教程但没有尝试多于一个低于C的级别:



还添加了OpenFileDialog.RestoreDirectory但它没有效果。



What I have tried:

Read many on line articles including Microsoft tutorial on using InitialDirectory but none try to go more than one level below C:

Also added OpenFileDialog.RestoreDirectory but it has no effect.

推荐答案

If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
   OpenFileDialog1.InitialDirectory = "C:\Race\PHRF List\"
   OpenFileDialog1.Filter = "txt files(*.txt)|*.txt"                   '
   txtPHRFfile.Text = OpenFileDialog1.FileName
   OpenFileDialog1.RestoreDirectory = True
End If

在设置初始目录之前打开对话框!

试试这个:

You open the dialog before you set the Initial Directory!
Try this:

OpenFileDialog1.InitialDirectory = "C:\Race\PHRF List\"
OpenFileDialog1.Filter = "txt files(*.txt)|*.txt"                   '
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
   txtPHRFfile.Text = OpenFileDialog1.FileName
   OpenFileDialog1.RestoreDirectory = True
End If


这篇关于如何使openfiledialog.initialdirectory打开整个路径而不仅仅是第一级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 06:51