Csharp/C Sharp by API/System.Windows.Forms/OpenFileDialog

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

new OpenFileDialog()

<source lang="csharp">

using System; using System.Windows.Forms; using System.IO; class MainClass {

 public static void Main() 
 {
   OpenFileDialog dlgOpen = new OpenFileDialog();
   if (dlgOpen.ShowDialog() == DialogResult.OK)
   {
     string s = dlgOpen.FileName;
     Console.WriteLine("Filename " + s);
     Console.WriteLine(" Created at " + File.GetCreationTime(s));
     Console.WriteLine(" Accessed at " + File.GetLastAccessTime(s));
   }
 }

}

 </source>


OpenFileDialog.CheckFileExists

<source lang="csharp"> using System; using System.Collections.Generic; using System.Windows.Forms; static class MainClass {

   [STAThread]
   static void Main()
   {
       OpenFileDialog dlg = new OpenFileDialog();
       dlg.Filter = "Rich Text Files (*.rtf)|*.RTF|" +
         "All files (*.*)|*.*";
       dlg.CheckFileExists = true;
       dlg.InitialDirectory = Application.StartupPath;
 
       if (dlg.ShowDialog() == DialogResult.OK)
       {
           Console.WriteLine(dlg.FileName);
           
       }
   }

}

 </source>


OpenFileDialog.FileOk

<source lang="csharp"> using System; using System.Drawing; using System.ruponentModel; using System.Windows.Forms; using System.IO; public class OpenFileDialogEvent{

   static OpenFileDialog openfiledlg1;
   public static void Main()
   {
       openfiledlg1 = new OpenFileDialog();
       openfiledlg1.FileOk += new CancelEventHandler(OnFileOpenOK);
       
       openfiledlg1.Filter = "C# files (*.cs)|*.cs|Bitmap files (*.bmp)|*.bmp";
       openfiledlg1.FilterIndex = 1;
       openfiledlg1.ShowDialog();
   }
   static void OnFileOpenOK(Object sender, CancelEventArgs e)
   {
       MessageBox.Show("You selected the file "+openfiledlg1.FileName);
   }

}

 </source>


OpenFileDialog.Filter

<source lang="csharp"> using System; using System.Drawing; using System.ruponentModel; using System.Windows.Forms; using System.IO; public class OpenFileDialogEvent{

   static OpenFileDialog openfiledlg1;
   public static void Main()
   {
       openfiledlg1 = new OpenFileDialog();
       openfiledlg1.FileOk += new CancelEventHandler(OnFileOpenOK);
       
       openfiledlg1.Filter = "C# files (*.cs)|*.cs|Bitmap files (*.bmp)|*.bmp";
       openfiledlg1.FilterIndex = 1;
       openfiledlg1.ShowDialog();
   }
   static void OnFileOpenOK(Object sender, CancelEventArgs e)
   {
       MessageBox.Show("You selected the file "+openfiledlg1.FileName);
   }

}

 </source>


OpenFileDialog.FilterIndex

<source lang="csharp"> using System; using System.Drawing; using System.ruponentModel; using System.Windows.Forms; using System.IO; public class OpenFileDialogEvent{

   static OpenFileDialog openfiledlg1;
   public static void Main()
   {
       openfiledlg1 = new OpenFileDialog();
       openfiledlg1.FileOk += new CancelEventHandler(OnFileOpenOK);
       
       openfiledlg1.Filter = "C# files (*.cs)|*.cs|Bitmap files (*.bmp)|*.bmp";
       openfiledlg1.FilterIndex = 1;
       openfiledlg1.ShowDialog();
   }
   static void OnFileOpenOK(Object sender, CancelEventArgs e)
   {
       MessageBox.Show("You selected the file "+openfiledlg1.FileName);
   }

}

 </source>


OpenFileDialog.InitialDirectory

<source lang="csharp">

using System; using System.Collections.Generic; using System.Windows.Forms; static class MainClass {

   [STAThread]
   static void Main()
   {
       OpenFileDialog dlg = new OpenFileDialog();
       dlg.Filter = "Rich Text Files (*.rtf)|*.RTF|" +
         "All files (*.*)|*.*";
       dlg.CheckFileExists = true;
       dlg.InitialDirectory = Application.StartupPath;
 
       if (dlg.ShowDialog() == DialogResult.OK)
       {
           Console.WriteLine(dlg.FileName);
           
       }
   }

}

 </source>


OpenFileDialog.Multiselect

<source lang="csharp"> using System; using System.Windows.Forms; public class OpenFileDialogSetFiles {

 public static void Main() 
 {
   OpenFileDialog dlgOpen = new OpenFileDialog();
   
   dlgOpen.Title = "Select one or more files";
   dlgOpen.ShowReadOnly = true;
   dlgOpen.Multiselect = true;
   if (dlgOpen.ShowDialog() == DialogResult.OK)
   {
     foreach (string s in dlgOpen.FileNames)
       Console.WriteLine(s);
   }
 }

}

 </source>


OpenFileDialog.RestoreDirectory

<source lang="csharp"> using System; using System.IO; using System.Windows.Forms; namespace nsStreams {

   public class ReadIn
   {
       [STAThread]
       static public void Main (string [] args)
       {
           OpenFileDialog fileOpen = new OpenFileDialog ();
           if (args.Length == 0)
           {
               fileOpen.InitialDirectory = ".\\";
               fileOpen.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
               fileOpen.FilterIndex = 0;
               fileOpen.RestoreDirectory = false; //true;

// if (fileOpen.ShowDialog () == DialogResult.Cancel)

               if (fileOpen.ShowDialog () != DialogResult.OK)
               {
                   return;
               }
           }
           else
           {
               fileOpen.FileName = args[0];
           }
           Stream strm;
           StreamReader reader;
           try
           {
               strm = fileOpen.OpenFile ();
               reader = new StreamReader (strm);
           }
           catch (Exception e)
           {
               string Message = e.Message + "\n\nCannot open "
                                + fileOpen.FileName;
               MessageBox.Show (Message, "Open error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
               return;
           }
           Console.Write (reader.ReadToEnd ());
           reader.Close ();
           strm.Close ();
       }
   }

}

 </source>


OpenFileDialog.ShowDialog()

<source lang="csharp"> using System; using System.Windows.Forms; using System.IO; class MainClass {

 public static void Main() 
 {
   OpenFileDialog dlgOpen = new OpenFileDialog();
   if (dlgOpen.ShowDialog() == DialogResult.OK)
   {
     FileInfo fi = new FileInfo(dlgOpen.FileName);
     Console.WriteLine("Filename " + fi.FullName );
     Console.WriteLine(" Created at " + fi.CreationTime );
     Console.WriteLine(" Accessed at " + fi.LastAccessTime );
   }
 }

}

 </source>


OpenFileDialog.ShowReadOnly

<source lang="csharp"> using System; using System.Windows.Forms; public class OpenFileDialogSetFiles {

 public static void Main() 
 {
   OpenFileDialog dlgOpen = new OpenFileDialog();
   
   dlgOpen.Title = "Select one or more files";
   dlgOpen.ShowReadOnly = true;
   dlgOpen.Multiselect = true;
   if (dlgOpen.ShowDialog() == DialogResult.OK)
   {
     foreach (string s in dlgOpen.FileNames)
       Console.WriteLine(s);
   }
 }

}

 </source>


OpenFileDialog.Title

<source lang="csharp"> using System; using System.Windows.Forms; public class MainClass {

   [STAThread]
 public static void Main() 
 {
   OpenFileDialog dlgOpen = new OpenFileDialog();
   
   dlgOpen.Title = "Select one or more files";
   dlgOpen.ShowReadOnly = true;
   dlgOpen.Multiselect = true;
   if (dlgOpen.ShowDialog() == DialogResult.OK)
   {
     foreach (string s in dlgOpen.FileNames)
       Console.WriteLine(s);
   }
 }

}

 </source>