2013-03-01 41 views
0

我有一个组合框,它从我放在一起的文件的名称中获取项目列表,其目的是为了使它动态 - 我对c#非常陌生,它没有对我来说不是一种方式。 - 下面是该位的代码:在组合框选择打开文件

string[] files = Directory.GetFiles(templatePath); 
     foreach (string file in files) 
      cbTemplates.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file)); 

基本上,这工作得很好,它填充我与我在该路径的文件的名称组合框,问题是,我需要打开选中后的文件在组合框中,阅读它的内容并将它们放在标签中,我想可能StreamReader会帮助我,但我不知道如何实现它,我搜索了互联网,但它看起来像没有人有过相同的想法我。有人可以请指点我正确的方向吗?一个类似的链接或我需要使用的对象的指南将是伟大的,谢谢!

+0

嗯,那真是一个非常新的想法。使用File.ReadAllLines函数而不是StreamReader – VladL 2013-03-01 21:26:57

回答

0

感谢您的所有帮助人员,但我想出了如何解决我的问题,我将发布未来事件的代码。 PD。对不起,我花了这么长的时间来回复,我正在度假

string[] fname = Directory.GetFiles(templatePath); // Gets all the file names from the path assigned to templatePath and assigns it to the string array fname 
     // Begin sorting through the file names assigned to the string array fname 
     foreach (string file in fname) 
     { 
      // Remove the extension from the file names and compare the list with the dropdown selected item 
      if (System.IO.Path.GetFileNameWithoutExtension(file) != cbTemplates.SelectedItem.ToString()) 
      { 
       // StreamReader gets the contents from the found file and assigns them to the labels 
       using (var obj = new StreamReader(File.OpenRead(file))) 
       { 
        lbl1.Content = obj.ReadLine(); 
        lbl2.Content = obj.ReadLine(); 
        lbl3.Content = obj.ReadLine(); 
        lbl4.Content = obj.ReadLine(); 
        lbl5.Content = obj.ReadLine(); 
        lbl6.Content = obj.ReadLine(); 
        lbl7.Content = obj.ReadLine(); 
        lbl8.Content = obj.ReadLine(); 
        lbl9.Content = obj.ReadLine(); 
        lbl10.Content = obj.ReadLine(); 
        obj.Dispose(); 
       } 
      } 
     } 
0

你应该做的是将文件的名称存储在一个单独的文件(csv或xml)中。然后使用此文件来加载组合框和作为索引器。例如

可以说你有文件a.txt,b.txt和c.txt。你应该(就像你已经)以编程方式读取文件名,然后以任何你想要的格式将它们写入一个新文件,包括一个唯一的索引方案(数字工作正常)。

您的CSV可能是这样的:

1, a.txt, 
2, b.txt, 
3, c.txt, 

从这里可以解析新创建的CSV根据自己的喜好。用它来填充你的组合框,索引是它的值和文本的文本。然后你可以阅读你的combobox selectedvalue,从csv索引中获取正确的文件名,最后打开文件。

它可能被延长,但它会工作。您也可以使用多维数组,但从教育的角度来看,这更有趣,它可以帮助您进行读/写操作。

0

要理解你的问题并不那么容易。你只想在组合框中显示文件名W/O扩展名吗?我希望这段代码对你有用。

internal class FileDetail 
{ 
    public string Display { get; set; } 
    public string FullName { get; set; } 
} 
public partial class Example: Form // This is just widows form. InitializeComponent is implemented in separate file. 
{ 
    public Example() 
    { 
     InitializeComponent(); 
     filesList.SelectionChangeCommitted += filesListSelectionChanged; 
     filesList.Click += filesListClick; 
     filesList.DisplayMember = "Display"; 
    }   
    private void filesListClick(object sender, EventArgs e) 
    { 
     var dir = new DirectoryInfo(_baseDirectory); 
     filesList.Items.AddRange(
      (from fi in dir.GetFiles() 
      select new FileDetail 
      { 
       Display = Path.GetFileNameWithoutExtension(fi.Name), 
       FullName = fi.FullName 
      }).ToArray() 
     ); 
    } 
    private void filesListSelectionChanged(object sender, EventArgs e) 
    {    
     var text = File.ReadAllText(
      (filesList.SelectedItem as FileDetail).FullName 
     ); 

     fileContent.Text = text; 
    } 
    private static readonly string _baseDirectory = @"C:/Windows/System32/"; 
}