FileDialog.Filter Propriété
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Obtient ou définit la chaîne de filtre de nom de fichier actuelle, qui détermine les choix qui apparaissent dans la zone « Enregistrer en tant que type de fichier » ou « Fichiers de type » dans la boîte de dialogue.
public:
property System::String ^ Filter { System::String ^ get(); void set(System::String ^ value); };
public string Filter { get; set; }
member this.Filter : string with get, set
Public Property Filter As String
Valeur de propriété
Options de filtrage de fichiers disponibles dans la boîte de dialogue.
Exceptions
Filter le format n’est pas valide.
Exemples
L’exemple de code suivant utilise l’implémentation et FileDialog illustre la création, le OpenFileDialog paramètre des propriétés et l’affichage de la boîte de dialogue. L’exemple utilise les propriétés et FilterIndex les Filter propriétés pour fournir une liste de filtres pour l’utilisateur. L’exemple nécessite un formulaire avec un Button formulaire placé sur celui-ci et l’espace System.IO de noms ajouté à celui-ci.
private:
void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
Stream^ myStream;
OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
openFileDialog1->InitialDirectory = "c:\\";
openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1->FilterIndex = 2;
openFileDialog1->RestoreDirectory = true;
if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
{
if ( (myStream = openFileDialog1->OpenFile()) != nullptr )
{
// Insert code to read the stream here.
myStream->Close();
}
}
}
var fileContent = string.Empty;
var filePath = string.Empty;
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.InitialDirectory = "c:\\";
openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//Get the path of specified file
filePath = openFileDialog.FileName;
//Read the contents of the file into a stream
var fileStream = openFileDialog.OpenFile();
using (StreamReader reader = new StreamReader(fileStream))
{
fileContent = reader.ReadToEnd();
}
}
}
MessageBox.Show(fileContent, "File Content at path: " + filePath, MessageBoxButtons.OK);
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myStream As Stream = Nothing
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
myStream = openFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
' Insert code to read the stream here.
End If
Catch Ex As Exception
MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
Finally
' Check this again, since we need to make sure we didn't throw an exception on open.
If (myStream IsNot Nothing) Then
myStream.Close()
End If
End Try
End If
End Sub
Remarques
Pour chaque option de filtrage, la chaîne de filtre contient une description du filtre, suivie de la barre verticale (|) et du modèle de filtre. Les chaînes pour différentes options de filtrage sont séparées par la barre verticale.
Voici un exemple de chaîne de filtre :
Text files (*.txt)|*.txt|All files (*.*)|*.*
Vous pouvez ajouter plusieurs modèles de filtre à un filtre en séparant les types de fichiers avec des points-virgules, par exemple :
Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*
Utilisez la propriété pour définir l’option FilterIndex de filtrage affichée en premier à l’utilisateur.