Csharp/C Sharp by API/System.Windows.Forms/OpenFileDialog — различия между версиями

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

Версия 15:31, 26 мая 2010

new OpenFileDialog()

 

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));
    }
  }
}


OpenFileDialog.CheckFileExists

 
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);
            
        }
    }
}


OpenFileDialog.FileOk

 
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);
    }
}


OpenFileDialog.Filter

 
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);
    }
}


OpenFileDialog.FilterIndex

 
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);
    }
}


OpenFileDialog.InitialDirectory

 

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);
            
        }
    }
}


OpenFileDialog.Multiselect

 
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);
    }
  }
}


OpenFileDialog.RestoreDirectory

 
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 ();
        }
    }
}


OpenFileDialog.ShowDialog()

 
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 );
    }
  }
}


OpenFileDialog.ShowReadOnly

 
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);
    }
  }
}


OpenFileDialog.Title

 
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);
    }
  }
}