OpenFileDialog : ouvrir un fichier à l'écran

Résolu
K29CD Messages postés 4 Date d'inscription mardi 15 juillet 2008 Statut Membre Dernière intervention 30 octobre 2009 - 20 mars 2009 à 17:29
K29CD Messages postés 4 Date d'inscription mardi 15 juillet 2008 Statut Membre Dernière intervention 30 octobre 2009 - 23 mars 2009 à 10:14
Bonjour à tous,

Mon formulaire contient un bouton "Parcourir" qui ouvre un OpenFileDialog.
L'explorateur de fichier s'ouvre bien et je peux sélectionner les fichiers (ici des fichiers .xls).
Malheureusement, quand je clique sur le fichier sélectioné ou sur "Ouvrir", rien ne se passe !
Je ne veux pas charger ce que contient le fichier .xls, ni même le parcourir, je veux SIMPLEMENT qu'EXCEL s'ouvre et m'affiche à l'écran le fichier... Or, rien de ceci ne se passe !

Voici mon code :


System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();

ofd.InitialDirectory = System.Windows.Forms.Application.ExecutablePath;

ofd.Filter = "Fichiers XLS (*.xls)|*.xls|Tous les fichiers (*.*)|*.*";



if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)

{

// ouverture du fichier

ofd.OpenFile() ;


}

SVP je suis nouveau en C#, merci de votre précieuse aide...
J'ai parcouru de nombreux forums (y compris celui-ci) sans réussir à comprendre où est mon erreur ou mon omission. J'ai besoin de vos lumières.

Cdlt

K29

3 réponses

cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
20 mars 2009 à 18:04
Salut,

La méthode OpenFile ouvre un Stream pour lire le contenu du fichier, elle ne lance pas le programme associé à l'extension.
Voir du côté de Process.Start pour celà.

/*
coq
MVP Visual C#
CoqBlog
*/
3
balousto Messages postés 2 Date d'inscription dimanche 1 février 2009 Statut Membre Dernière intervention 20 mars 2009
20 mars 2009 à 19:02
Slt ...
J'espère que ça va t'aider.

if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)

{

 // il lance le fichier choisi (ofd.filename) si windows ouvre les fichiers .xls est via ExCel
System.Diagnostics.Process.Start("ofd..FileName")

}
3
K29CD Messages postés 4 Date d'inscription mardi 15 juillet 2008 Statut Membre Dernière intervention 30 octobre 2009 1
23 mars 2009 à 10:14
Merci à tous les 2 !
Vos réponses m'ont parfaitement éclairé et se complètent l'une l'autre.

Voici donc le code complété :

[STAThread]
private void b_Parcourir_Click(object sender, EventArgs e)
{

try
{

System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
ofd.InitialDirectory = System.Windows.Forms.Application.ExecutablePath;
ofd.Filter = "Fichiers XLS (*.xls)|*.xls|Tous les fichiers (*.*)|*.*";


if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Process process;
string file = ofd.SafeFileName.ToString();
ProcessStartInfo startInfo = new ProcessStartInfo();
// définition du type de programme exécutant --> EXCEL
startInfo.FileName = "excel.exe";
startInfo.WindowStyle = ProcessWindowStyle.Maximized;
startInfo.ErrorDialog = true;

try
{
// ouverture dans EXCEL du fichier sélectionné
process = Process.Start(file);

}//Fin TRY

catch (Exception ex)
{
MessageBox.Show("Could not start process.\n" + ex.Message);

}// Fin CATCH

}// Fin IF

}// Fin TRY

catch (Exception e4)
{
MessageBox.Show("Erreur d'ouverture du fichier:\n"
+ e4.Message);
}// Fin CATCH

}// Fin void


MERCI BEAUCOUP !!!!!

Cordialement

K29
1
Rejoignez-nous