本文整理汇总了C#中Microsoft.Win32.OpenFileDialog.OpenFile方法的典型用法代码示例。如果您正苦于以下问题:C# OpenFileDialog.OpenFile方法的具体用法?C# OpenFileDialog.OpenFile怎么用?C# OpenFileDialog.OpenFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Win32.OpenFileDialog
的用法示例。
在下文中一共展示了OpenFileDialog.OpenFile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ButtonBase_OnClick
private void ButtonBase_OnClick (object sender, RoutedEventArgs e)
{
try
{
var dig = new OpenFileDialog();
dig.Title = "选择文件";
dig.Filter = "rtf文件|*.rtf";
dig.RestoreDirectory = true;
if(dig.ShowDialog() == true)
{
using(var ms = dig.OpenFile())
{
var bytes = new byte[ms.Length];
ms.Read(bytes, 0, (int)ms.Length);
var str = Encoding.UTF8.GetString(bytes);
this.RtfText = str;
}
}
} catch(Exception c)
{
MessageBox.Show(c.Message);
}
}
示例2: SelectFileButton_Click
private void SelectFileButton_Click(object sender, RoutedEventArgs e)
{
// TODO 1 open file dialog and choose directoy to start
OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory = "C:\\Users\\Jason\\Downloads";
if (dialog.ShowDialog() == true)
{
try
{
using (StreamReader reader = new StreamReader(dialog.OpenFile()))
{
string line;
StringBuilder sb = new StringBuilder();
while ((line = reader.ReadLine()) != null)
{
sb.Append(line).Append("...").Append(Environment.NewLine);
}
ContentTb.Text = sb.ToString();
}
}
catch (IOException ex)
{
StatusLb.Content = ex.Message;
}
}
// TODO 2 if the user selected a file display file/dir name and text
}
示例3: LoadLibButton_Click
private void LoadLibButton_Click(object sender, RoutedEventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "Dynamically Linked Library|*.dll";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == true)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
LibraryPath = openFileDialog1.FileName;
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
示例4: LoadDocumentToInsert
private RadDocument LoadDocumentToInsert()
{
RadDocument document = null;
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Word Documents (*.docx)|*.docx|All Files (*.*)|*.*";
if (ofd.ShowDialog() == true)
{
string extension;
#if SILVERLIGHT
extension = ofd.File.Extension.ToLower();
#else
extension = Path.GetExtension(ofd.SafeFileName).ToLower();
#endif
IDocumentFormatProvider provider = DocumentFormatProvidersManager.GetProviderByExtension(extension);
Stream stream;
#if SILVERLIGHT
stream = ofd.File.OpenRead();
#else
stream = ofd.OpenFile();
#endif
using (stream)
{
document = provider.Import(stream);
}
}
return document;
}
示例5: GetLayout
public override InteriorField[,] GetLayout()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.DefaultExt = "txt";
DirectoryInfo currentDir = new DirectoryInfo(Environment.CurrentDirectory);
openFileDialog.InitialDirectory = (currentDir.EnumerateDirectories().FirstOrDefault(d => d.Name == DataSubdirName) ?? currentDir).FullName;
if (openFileDialog.ShowDialog().GetValueOrDefault())
{
this.LayoutIdentifier = openFileDialog.FileName;
using (StreamReader readLines = new StreamReader(openFileDialog.OpenFile()))
{
List<List<InteriorField>> layout = readLines
.ReadToEnd()
.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s
.Select(c => this.CreateField(c)).ToList()).ToList();
int columnsCount = layout.Max(l => l.Count);
InteriorField[,] result = new InteriorField[layout.Count, columnsCount];
for (int row = 0; row < layout.Count; row++)
{
for (int col = 0; col < layout[row].Count; col++)
{
result[row, col] = layout[row][col];
}
}
return result;
}
}
else
{
return null;
}
}
示例6: ReadFile
public static string ReadFile(this SerialPort com)
{
Stream input = null;
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = Environment.CurrentDirectory;
openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == true)
{
try
{
if ((input = openFileDialog.OpenFile()) != null)
using (StreamReader sr = new StreamReader(input))
{
string text = sr.ReadToEnd();
byte [] buffer = ASCIIEncoding.ASCII.GetBytes(text);
com.Write(buffer, 0, buffer.Length);
return string.Format("File {0}: \n{1}\n", openFileDialog.FileName, text);
}
}
catch (Exception ex)
{
return "Error: Could not read file from disk. Original error: " + ex.Message;
}
}
return "File isn`t selected.\n";
}
示例7: ImportDB_Click
private void ImportDB_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.DefaultExt = ".db";
dialog.Filter = "DataBase(.db)|*.db";
bool? res = dialog.ShowDialog();
if (res.HasValue && res.Value) {
DBConnection.ins().closeDb();
Stream stream = dialog.OpenFile();
FileStream fs = new FileStream(DBConnection.path, FileMode.OpenOrCreate);
byte[] buffer = new byte[1024];
int count = 0;
while ((count = stream.Read(buffer, 0, 1024)) > 0)
{
fs.Write(buffer, 0, count);
}
stream.Close();
fs.Close();
MessageBoxResult result = MessageBox.Show("匯入資料庫需重開系統", "確認視窗", MessageBoxButton.OK, MessageBoxImage.Information);
if (result == MessageBoxResult.OK) {
System.Diagnostics.Process.Start(System.Windows.Application.ResourceAssembly.Location);
System.Windows.Application.Current.Shutdown();
}
}
}
示例8: OpenFile
public Stream OpenFile(string defaultExtension, string filter)
{
var fd = new OpenFileDialog { DefaultExt = defaultExtension, Filter = filter };
var result = fd.ShowDialog();
return result != null && result.Value ? fd.OpenFile() : null;
}
示例9: OpenBrowserDialog
public string OpenBrowserDialog(string defaultPath)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = defaultPath;
ofd.OpenFile();
return ofd.FileName;
}
示例10: ChooseButton_OnClick
private void ChooseButton_OnClick(object sender, RoutedEventArgs e)
{
var dlg = new OpenFileDialog();
dlg.Filter = "(*.bmp, *.jpg, *.png)|*.bmp;*.jpg;*.png";
if (dlg.ShowDialog() == true)
{
this.ViewModel.BinaryData = dlg.OpenFile().ToArray();
}
}
示例11: openfile
/// <summary>
///
/// </summary>
/// <returns></returns>
public Stream openfile()
{
var dialog = new OpenFileDialog();
if (dialog.ShowDialog() == true)
{
return dialog.OpenFile();
}
return null;
}
示例12: ChangeStyleCommand_Executed
private void ChangeStyleCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "XAML Styles (*.xaml)|*.xaml";
dlg.InitialDirectory = Environment.CurrentDirectory;
if (dlg.ShowDialog().Value)
{
using (Stream fs = dlg.OpenFile())
{
LoadStyle(fs);
}
}
}
示例13: LoadASM
private void LoadASM(object sender, RoutedEventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.FileName = "";
dialog.DefaultExt = ".asm";
dialog.Filter = "Assembly (.asm)|*.asm|Text Files (.txt)|*.txt";
bool? res = dialog.ShowDialog();
if(res == true)
{
StreamReader f = new StreamReader(dialog.OpenFile());
Assembly.Text = f.ReadToEnd();
f.Close();
}
}
示例14: uploadBtn_Click
private void uploadBtn_Click(object sender, RoutedEventArgs e)
{
var fileDlg = new OpenFileDialog();
if (fileDlg.ShowDialog() == true)
{
new FileInfo(fileDlg.FileName);
using (Stream s = fileDlg.OpenFile())
{
attachTxtBox.Text = fileDlg.FileName;
}
}
}
示例15: Open
public static void Open(string filePath, ref ObservableCollection<Achievement> achievements)
{
var openDialog = new OpenFileDialog {Filter = "Data files (*.save)|*.save;"};
var ok = openDialog.ShowDialog();
if (!ok.Value) return;
_currentFilePath = openDialog.FileName;
using (var fs = openDialog.OpenFile())
{
var serializer = new XmlSerializer(typeof(ObservableCollection<Achievement>));
achievements = (ObservableCollection<Achievement>)serializer.Deserialize(fs);
}
}