如何在将文件插入Listbox时记住最后一个路径

时间:2012-03-22 11:25:38

标签: c# winforms

我有Listbox和文件的应用程序,每次按下Add按钮时默认的C盘打开,我希望应用程序记住我使用的最后一条路径

private void btnAdd_Click(object sender, EventArgs e)
{
    System.IO.Stream myStream;
    OpenFileDialog thisDialog = new OpenFileDialog();
    thisDialog.InitialDirectory = "c:\\";
    thisDialog.Filter = "(*.snoop, *.pcap, *.cap, *.net)|*.snoop; *.pcap; *.cap; *.net|" + "All files (*.*)|*.*";
    thisDialog.FilterIndex = 1;
    thisDialog.RestoreDirectory = false;
    thisDialog.Multiselect = true; // Allow the user to select multiple files
    thisDialog.Title = "Please Select Source File";
    thisDialog.FileName = lastPath;
    List<string> list = new List<string>();

    if (thisDialog.ShowDialog() == DialogResult.OK)
    {
        foreach (String file in thisDialog.FileNames)
        {
            try
            {
                if ((myStream = thisDialog.OpenFile()) != null)
                {
                    using (myStream)
                    {
                        listBoxFiles.Items.Add(file);
                    }
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
        }

    }
}

5 个答案:

答案 0 :(得分:2)

保存全局变量中使用的最后一个目录,如下所示:

private string _lastPath = string.Empty;

然后在文件选择后初始化它:

if(thisDialog.Filenames.Length > 0)
    _lastPath = Path.GetDirectoryName(thisDialog.Filenames[0]);

当您重新打开对话框时,使用此检查设置InitialDirectory:

thisDialog.InitialDirectory = (_lastPath.Length > 0 ? _lastPath: "c:\\"); 

并删除thisDialog.FileName = lastPath;

编辑---更新你的代码---

// This at the global level of your form 
private string _lastPath = string.Empty;**

private void btnAdd_Click(object sender, EventArgs e) 
{ 
    System.IO.Stream myStream; 
    OpenFileDialog thisDialog = new OpenFileDialog(); 
    thisDialog.InitialDirectory = (_lastPath.Length > 0 ? _lastPath: "c:\\");
    thisDialog.Filter = "(*.snoop, *.pcap, *.cap, *.net)|*.snoop; *.pcap; *.cap; *.net|" + "All files (*.*)|*.*"; 
    thisDialog.FilterIndex = 1; 
    thisDialog.RestoreDirectory = false; 
    thisDialog.Multiselect = true; // Allow the user to select multiple files 
    thisDialog.Title = "Please Select Source File"; 
    thisDialog.FileName = lastPath; 
    List<string> list = new List<string>(); 

    if (thisDialog.ShowDialog() == DialogResult.OK) 
    { 
        if(thisDialog.Filenames.Length > 0) 
            _lastPath = Path.GetDirectoryName(thisDialog.Filenames[0]);

        foreach (String file in thisDialog.FileNames) 
        { 
            try 
            { 
                if ((myStream = thisDialog.OpenFile()) != null) 
                { 
                    using (myStream) 
                    { 
                        listBoxFiles.Items.Add(file); 
                    } 
                } 
            } 

            catch (Exception ex) 
            { 
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); 
            } 
        } 

    } 
} 

答案 1 :(得分:2)

您可以使用Visual Studio在每次执行应用程序时都有最后一个路径值。

只需转到Project Properties-&gt; Configuration并添加值描述符。

示例:

Name = LastPath; Type = string;范围=用户;值=“默认路径”;

然后在重建yout应用程序之后,可以这样设置此属性:

Settings.Default.LastPath = LastPathSelected;

稍后,您可以使用以下方法检索值:

thisDialog.InitialDirectory = Settings.Default.LastPath;

答案 2 :(得分:1)

thisDialog.InitialDirectory = Path.GetDirectoryName(lastPath);

答案 3 :(得分:0)

是的,您可以使用OpenFileDialog.InitialDirectory属性。注意:您正在设置目录而不是文件。因此,请务必从路径中删除文件名。

more info here

答案 4 :(得分:-1)

删除此行,您有最后一条路径

thisDialog.InitialDirectory = "c:\\";