C# OpenFileDialog Lock To Directory

后端 未结 4 1632
萌比男神i
萌比男神i 2020-12-04 01:52

I am making a software that needs to ONLY be able allow people to select files and folders using the OpenFileDialog that are in the same directory as the program and that ar

4条回答
  •  没有蜡笔的小新
    2020-12-04 02:27

    This is how I did it.

       openFileDialog1.InitialDirectory = Path.Combine(Path.GetDirectoryName(Application.StartupPath), "FolderName");
    
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {            
                    while(Path.GetDirectoryName(openFileDialog1.FileName) != Path.Combine(Path.GetDirectoryName(Application.StartupPath), "FolderName")){
    
                        MessageBox.Show("Please select .EXE which is in the default folder", "Wrong folder", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    openFileDialog1.ShowDialog();
    
                }                       
            }
    

提交回复
热议问题