C#WPF ListBox打开文件并保存打开的文件路径 [英] C# WPF ListBox Open files and save opened file path
本文介绍了C#WPF ListBox打开文件并保存打开的文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好。 İm在一个项目上工作,但不能写他们的代码。这意味着当我打开文件应用程序必须获取文件路径并保存在列表框中。 App重新打开应该读取已保存的文件路径
解决方案
private < span class =code-keyword> void btnOpenFiles_Click( object sender,RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true ;
openFileDialog.Filter = 文本文件(* .txt)| * .txt |所有文件(*。 。*)| *。*跨度>;
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (openFileDialog.ShowDialog()== true )
{
foreach (字符串文件名 openFileDialog .FileNames)
lbFiles.Items.Add(Path.GetFileName(filename));
}
}
我的代码在这里但没有工作当我把文件拖到窗口时,窗口必须得到文件路径并保存在列表框中你帮我吗?
<前lang =cs> 私人 void listbox1_DragEnter( object sender,DragEventArgs e)
{
if (e。 Data.GetDataPresent(DataFormats.FileDrop))
e.Effects = DragDropEffects.All;
else
e.Effects = DragDropEffects.None;
}
private void listbox1_Drop(< span class =code-keyword> object sender,DragEventArgs e)
{
string [] s =( string [])e.Data.GetData(DataFormats.FileDrop, false );
int i;
for (i = 0 ; i < s.Length; i ++)
listbox1.Items.Add(s [i]);
for (i = 0 ; i < s.Length; i ++)
listbox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(s [i]));
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
}
}
void listbox1_SelectionChanged(< span class =code-keyword> object sender,SelectionChangedEventArgs e)
{
try
{
string dir = Environment.CurrentDirectory + @ \\ \\Files\MyFiles\跨度>;
string file = listbox1.SelectedItem.ToString();
System.Diagnostics.Process.Start(dir + file);
}
catch
{
MessageBox.Show( 加载文件时出错。 + Environment.NewLine + 找不到文件。, 错误,MessageBoxButton.OK);
}
}
void b_Click( object sender,RoutedEventArgs e)
{
string fileName =((Button)sender)。 Tag.ToString();
System.Diagnostics.Process.Start(fileName);
}
private void btnSave_Click( object sender,RoutedEventArgs e)
{
const string sPath = save.txt;
System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(sPath);
foreach ( var item in listbox1.Items)
{
SaveFile.WriteLine(item);
}
SaveFile.Close();
MessageBox.Show( 程序已保存!);
}
Hi everyone. İm working on one project but İ cant write them code. İ mean when im opened file Application must get file path and save in listboxbox. While App re opening should read saved file path
解决方案
private void btnOpenFiles_Click(object sender, RoutedEventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Multiselect = true; openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); if(openFileDialog.ShowDialog() == true) { foreach(string filename in openFileDialog.FileNames) lbFiles.Items.Add(Path.GetFileName(filename)); } }
My code here but not working When im draged file to window , window must get file path and save in listbox can u help me ?
private void listbox1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effects = DragDropEffects.All; else e.Effects = DragDropEffects.None; } private void listbox1_Drop(object sender, DragEventArgs e) { string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false); int i; for (i = 0; i < s.Length; i++) listbox1.Items.Add(s[i]); for (i = 0; i < s.Length; i++) listbox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(s[i])); if (e.Data.GetDataPresent(DataFormats.FileDrop)) { } } void listbox1_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { string dir = Environment.CurrentDirectory + @"\Files\MyFiles\"; string file = listbox1.SelectedItem.ToString(); System.Diagnostics.Process.Start(dir + file); } catch { MessageBox.Show("Error Loading File." + Environment.NewLine + "File Not Found.", "Error", MessageBoxButton.OK); } } void b_Click(object sender, RoutedEventArgs e) { string fileName = ((Button)sender).Tag.ToString(); System.Diagnostics.Process.Start(fileName); } private void btnSave_Click(object sender, RoutedEventArgs e) { const string sPath = "save.txt"; System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(sPath); foreach (var item in listbox1.Items) { SaveFile.WriteLine(item); } SaveFile.Close(); MessageBox.Show("Programs saved!"); }
这篇关于C#WPF ListBox打开文件并保存打开的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文