简体   繁体   中英

Can OpenFileDialog Filter Out Certain Files By Name?

First, I have looked at this post . One of the answers seems to offer hope for filtering in ShowDialog by name. Now, here is a description of what I am trying to do:

I have this bit of C# code:

private System.Windows.Forms.OpenFileDialog csv_file_open_dlg;
.
.
.
  csv_file_open_dlg.FileName = "";
  csv_file_open_dlg.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*";
  int rc = 0;


  if (csv_file_open_dlg.ShowDialog() == DialogResult.OK)
  {
     if (0 == m_csv_file_cmp_counter)
     {
        m_firstCsvFileSelected = csv_file_open_dlg.FileName;
     }
.
.
.

显示在ShowDialog中的文件

If I select the first file -- May 18 Muni RosterDetailReport.csv -- and preserve its name in a string variable, is there a way to filter that file out the next time ShowDialog is run in the same directory?

In other words, after selecting May 18 Muni RosterDetailReport.csv , is there a way to feed that name back into ShowDialog , as a filter?

I think the answer is no, but I am just double-checking. If not, is there a workaround by subscribing to the OpenFileDialog event as indicated in the post I noted at the beginning of this post?

Edit:

So it sounds like, I could use the OK event to prevent the user from selecting the first file a second time? I am hoping someone will answer this with an answer.

Given it is not possible to filter by file name in OpenFileDialog , this is what I did to prevent the user from loading the same file twice:

string m_firstFileLoaded; // a member variable of the class.
.
.
.
private void LoadCsvFile_Click(object sender, EventArgs e)
{
    printb.Enabled = false;
    csv_file_open_dlg.FileName = "";
    csv_file_open_dlg.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*";
    int rc = 0;

    if (csv_file_open_dlg.ShowDialog() == DialogResult.OK)
    {
        if (0 == m_csv_file_cmp_counter)
        {
            m_firstFileLoaded = csv_file_open_dlg.FileName;
            m_ComparisionDirection = -1; // Resets first and second file toggling.
            m_csv_file_cmp_counter = 0;
            m_csv_file_first.set_csv_path(csv_file_open_dlg.FileName);
            rc = m_csv_file_first.map_csv();
            LoadCsvFile.Text = "Load next csv file";
            m_csv_file_cmp_counter++;
        }
        else
        {
            // If the file is already chosen, throw up a warning.
            if (0 == String.Compare(m_firstFileLoaded, csv_file_open_dlg.FileName))
            {
                MessageBox.Show("You have already loaded " + csv_file_open_dlg.FileName + " . Please select another file", "Attempt to reload first file", MessageBoxButtons.OK);
            }
            else
            {

                m_csv_file_next.set_csv_path(csv_file_open_dlg.FileName);
                rc = m_csv_file_next.map_csv();
                LoadCsvFile.Text = "Files loaded.";
                LoadCsvFile.Enabled = false;
                start_compare.Enabled = true;
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM