當前位置: 首頁>>代碼示例>>C#>>正文


C# OpenFileDialog.OpenFile方法代碼示例

本文整理匯總了C#中System.Windows.Forms.OpenFileDialog.OpenFile方法的典型用法代碼示例。如果您正苦於以下問題:C# OpenFileDialog.OpenFile方法的具體用法?C# OpenFileDialog.OpenFile怎麽用?C# OpenFileDialog.OpenFile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Windows.Forms.OpenFileDialog的用法示例。


在下文中一共展示了OpenFileDialog.OpenFile方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: _ouvrir

        public string _ouvrir(typeFichier tf)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (tf == typeFichier.musique)
            {
                ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
                ofd.Filter = "mp3 files (*.mp3)|*.mp3";
                ofd.FilterIndex = 2;
                ofd.RestoreDirectory = true;

                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    if (ofd.OpenFile() != null)
                    {
                        try
                        {
                            Lecteur = new Audio(ofd.FileName, false);

                        }
                        catch (Exception esx)
                        {
                            MessageBox.Show("Fichier non reconnu." + esx.HResult);
                            return null;
                        }

                        return ofd.FileName;

                    }
                }
                return null;
            }
            else if (tf == typeFichier.image)
            {
                ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
                ofd.Filter = "JPG (*.jpg)|*.jpg";
                ofd.FilterIndex = 2;
                ofd.RestoreDirectory = true;

                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        if (ofd.OpenFile() != null)// On attribue le chemin du fichier à lire au
                        {

                            return ofd.FileName;

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

            }
            return null;
        }
開發者ID:joanny,項目名稱:Lecteur_mp3,代碼行數:60,代碼來源:ControleLecture.cs

示例2: button1_Click

        private void button1_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = "c:\\";
            ofd.Filter = "txt files (*.txt)|*.txt";
            ofd.RestoreDirectory = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = ofd.OpenFile()) != null)
                    {
                        StreamReader reader = new StreamReader(myStream);
                        str = reader.ReadToEnd();
                        inputBox1.Text = str;

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error:" + ex.Message);
                }
            }
        }
開發者ID:kaxaru,項目名稱:Euler,代碼行數:25,代碼來源:Form1.cs

示例3: SelectFileToAdjuct

 internal bool SelectFileToAdjuct(WfAdjunct adjunct)
 {
     if (adjunct == null)
     {
         throw new NullReferenceException("WfAdjunct cannot be null");
     }
     OpenFileDialog dialog = new OpenFileDialog();
     dialog.Title = "請選擇附件";
     dialog.Filter = "Word 文件 (*.doc)|*.txt|All files (*.*)|*.*";
     dialog.FilterIndex = 2;
     if (dialog.ShowDialog() == DialogResult.OK)
     {
         Stream stream = dialog.OpenFile();
         if (stream != null)
         {
             if (adjunct.File == null)
             {
                 adjunct.File = new WfAdjunctFile(adjunct);
             }
             adjunct.File.Data = Read2Buffer(stream, -1);
             stream.Close();
             adjunct.Type = Path.GetExtension(dialog.FileName);
             if (StringHelper.IsNull(adjunct.Name))
             {
                 adjunct.Name = Path.GetFileNameWithoutExtension(dialog.FileName);
             }
             return true;
         }
     }
     return false;
 }
開發者ID:vanloc0301,項目名稱:mychongchong,代碼行數:31,代碼來源:AdjunctEditCommand.cs

示例4: button2_Click

        private void button2_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            //openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "jpg files (*.jpg)|*.jpg|png files (*.png)|*.png|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        using (myStream)
                        {
                            Image theImage = Image.FromStream(myStream);
                            OpenObject objForm = new OpenObject(this);
                            objForm.setImage(theImage);
                            objForm.ShowDialog();
                            //this.pictureBox2.Image = Image.FromStream(myStream);
                            myStream.Close();

                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        }
開發者ID:hanjae,項目名稱:VISPJ,代碼行數:32,代碼來源:Form1.cs

示例5: btnLoadCsvToPool_Click

        private void btnLoadCsvToPool_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "CSV files (*.csv)|*.csv";
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                using (StreamReader sr = new StreamReader(ofd.OpenFile()))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (IsFormClosing)
                            break;

                        string[] csvTokens = line.Split(',');
                        //if (Convert.ToInt32(csvTokens[0]) > 30000)
                        //{
                            CM.AddUrl("http://" + csvTokens[1]);
                            Application.DoEvents();
                            toolStripStatusLabel2.Text =
                                string.Format("Loading alexa url: {0}", csvTokens[0]);
                        //}
                    }

                    toolStripStatusLabel2.Text = string.Empty;
                    sr.Close();
                }
            }
        }
開發者ID:fcbaconguis,項目名稱:c,代碼行數:29,代碼來源:Form1.cs

示例6: showDialog

 public static void showDialog()
 {
     var d = new OpenFileDialog();
     if(d.ShowDialog() == DialogResult.OK){
         Console.WriteLine(d.OpenFile().ToString());
     }
 }
開發者ID:MrFlark,項目名稱:CSharp,代碼行數:7,代碼來源:Test.cs

示例7: GetFilePath

        public static bool GetFilePath(out String path)
        {
            Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;
            path = null;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        path = openFileDialog1.FileName;
                        myStream.Dispose();
                        return true;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);

                }
            }
            return false;
        }
開發者ID:theVitaliy,項目名稱:Garmash,代碼行數:30,代碼來源:FilePathGeter.cs

示例8: openToolStripMenuItem_Click

        // Open option on File menu
        // Purpose: Opens a file, reads in data to Employee object
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Stream fileStream = null;
            OpenFileDialog openFile = new OpenFileDialog();

            openFile.InitialDirectory = "C:\\";
            openFile.Filter = "text files (*.txt) | *.txt";

            if (openFile.ShowDialog() == DialogResult.OK)
            {
                if ((fileStream = openFile.OpenFile()) != null)
                {
                    StreamReader data = new StreamReader(fileStream);

                    string name = data.ReadLine();
                    int empNo = int.Parse(data.ReadLine());
                    string address = data.ReadLine();
                    string phone = data.ReadLine();
                    double hours = double.Parse(data.ReadLine());
                    double wage = double.Parse(data.ReadLine());

                    currentEmployee = new Employee(name, empNo, address, phone, wage, hours);

                    txtName.Text = currentEmployee.Name;
                    txtEmployeeNumber.Text = currentEmployee.EmployeeNumber.ToString();
                    txtAddress.Text = currentEmployee.Address;
                    txtPhone.Text = currentEmployee.Phone;
                    txtHours.Text = currentEmployee.Hours.ToString();
                    txtHourlyWage.Text = String.Format("{0:C}",currentEmployee.HourlyRate);
                }
            }
        }
開發者ID:kacecode,項目名稱:SchoolWork,代碼行數:34,代碼來源:Form1.cs

示例9: pbImage_DoubleClick

 private void pbImage_DoubleClick(object sender, System.EventArgs e)
 {
     OpenFileDialog ofd = new OpenFileDialog();
     Stream stream = null;
     ofd.Filter = "Image Files(*.bmp;*.jpg;*.png;*.jpeg)|*.bmp;*.jpg;*.png;*.jpeg|All files (*.*)|*.*";
     ofd.AutoUpgradeEnabled = true;
     if (ofd.ShowDialog() == DialogResult.OK)
     {
         try
         {
             if((stream = ofd.OpenFile()) != null)
             {
                 using (stream)
                 {
     #if DEBUG
                     MessageBox.Show("Not implement yet.");
     #endif
                 }
             }
         }
         catch (Exception ex)
         {
             MessageBox.Show("Error: Could not read file. Original error: " + ex.Message);
         }
     }
 }
開發者ID:wpscott,項目名稱:csis3275,代碼行數:26,代碼來源:ProductEdit.cs

示例10: btnImportFile_Click

 private void btnImportFile_Click(object sender, EventArgs e)
 {
     try
     {
         OpenFileDialog dialog = new OpenFileDialog();
         if (dialog.ShowDialog() == DialogResult.OK)
         {
             using (Stream fs = dialog.OpenFile())
             {
                 if (fs.Length > (long)Int32.MaxValue)
                 {
                     throw new Exception(string.Format("File is large than {0} KB", Math.Round(Int32.MaxValue / 1024.0, 3)));
                 }
                 txtValue.Text = string.Format("The content of file {0}", dialog.FileName);
                 using (BinaryReader reader = new BinaryReader(fs))
                 {
                     mByteData = reader.ReadBytes((int)fs.Length);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
開發者ID:gnllk,項目名稱:RedisClient,代碼行數:26,代碼來源:EditForm.cs

示例11: btnOpenXml_Click

        private void btnOpenXml_Click(object sender, EventArgs e)
        {
            LoadXML xml = new LoadXML();
            string pfad = null;
            //Öffnet OpenFileDialog
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;
            DialogResult result = openFileDialog1.ShowDialog();
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (openFileDialog1.OpenFile() != null)
                {
                    //Öffnet Explorer und ermittelt den Eingegebenen Dateipfad
                    string file = openFileDialog1.FileName;
                    try
                    {
                        pfad = openFileDialog1.FileName;
                    }
                    //TODO: Ausnahme hinzufügen
                    catch (IOException)
                    {

                    }
                }
                //Dateipfad wird an die Klasse ladeXML übergeben
                Console.WriteLine(result);
                Console.WriteLine(pfad);
                xml.ladeXML(pfad);

            }
        }
開發者ID:vikinger,項目名稱:ibsys2tool,代碼行數:35,代碼來源:UserControl_Prognose.cs

示例12: btnProcurar_Click

        private void btnProcurar_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtarquivo.Text = openFileDialog1.FileName;
                try
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        using (myStream)
                        {

                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        }
開發者ID:mppassos,項目名稱:Projetos-C--,代碼行數:29,代碼來源:Form1.cs

示例13: fileLoad

        private void fileLoad()
        {
            OpenFileDialog open = new OpenFileDialog();
            Stream stre;
            open.DefaultExt = "txt";
            open.Filter = "文字檔(*.txt)|*.txt";
            open.ReadOnlyChecked = true;
            if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                is_file_path2 = open.FileName;
                stre = open.OpenFile();
                try
                {
                    richTextBox1.LoadFile(stre, RichTextBoxStreamType.PlainText);
                }
                catch (Exception file_exce)
                {
                    MessageBox.Show(file_exce.ToString(), "錯誤訊息");
                }
                finally
                {
                    //關閉stream才算關閉這個檔案,隻有對openfiledialog做dispose是沒用的,
                    //還是會出現有人正在使用這個檔案
                    stre.Close();

                    open.Reset();
                    open.Dispose();
                    open = null;
                }
            }
        }
開發者ID:uiloon,項目名稱:CS_WindowTest,代碼行數:31,代碼來源:Form2.cs

示例14: buttonEscogerImagen_Click

        private void buttonEscogerImagen_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "Image files (*.jpg)|*.jpg|Image files (*.png)|*.png";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        using (myStream)
                        {
                            string imagen;
                            imagen = openFileDialog1.FileName;
                            pictureBoxImagen.ImageLocation = imagen;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        }
開發者ID:pablovargan,項目名稱:winforms-ooh4ria,代碼行數:30,代碼來源:AltaProducto.cs

示例15: openToolStripMenuItem_Click

 private void openToolStripMenuItem_Click(object sender, EventArgs e)
 {
     // Open file
     Stream myStream = null;
     OpenFileDialog openFileDialog1 = new OpenFileDialog();
     openFileDialog1.InitialDirectory = @"D:\";
     openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*) |*.*";
     openFileDialog1.FilterIndex = 2;
     if (openFileDialog1.ShowDialog() == DialogResult.OK)
     {
         try
         {
             if ((myStream = openFileDialog1.OpenFile()) != null)
             {
                 using (myStream)
                 {
                     richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                 }
             }
         }
         catch (Exception ex)
         {
             MessageBox.Show("Error : Could not read file from disk:	" + ex.Message);
         }
     }
 }
開發者ID:Helixanon,項目名稱:labs,代碼行數:26,代碼來源:Form1.cs


注:本文中的System.Windows.Forms.OpenFileDialog.OpenFile方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。