petule.08
petule.08

Reputation: 41

Display the file name while preserving the path

I need help.I would like to display only the file name in LisetView but when I clicked in the listView the file opened. But I need the full path to the file to open the file. How can I solve this?

private void button1_Click(object sender, EventArgs e)
    {
        llistView1.Items.Clear();IEnumerable<string> filesOrDirectories = FileDirectorySearcher.Search(@"D:\", "*" + textBox1.Text + "*")
                                                                       .Select(d => Path.ChangeExtension(d, null));                                                                    

foreach (string fileOrDirectory in filesOrDirectories){

DateTime dt = File.GetLastWriteTime(fileOrDirectory);
listView1.Items.Add(fileOrDirectory); 
ListViewItem lvi = new ListViewItem();
lvi.SubItems.Add(dt.ToString());
listView1.Items.Add(lvi);

}

private void listView1_ItemActivate(object sender, EventArgs e)
    {
        string currentDir = Directory.GetCurrentDirectory();
        string selectedFile = listView1.SelectedItems[0].Text; sloupci
        System.Diagnostics.Process.Start(Path.Combine(currentDir, selectedFile));

        

        

Upvotes: 2

Views: 129

Answers (1)

Karen Payne
Karen Payne

Reputation: 5102

Taking .Tag recommendation, the following code displays file names and last write in a ListView and shows how to get at .Tag details.

Note that this does not follow you current code but provides enough to display information and obtain information for the current item in MouseDoubleClick event which can be replaced with ItemActivate event.

Code to get files

public static class Extensions
{
    public static IEnumerable<FileInfo> GetFilesByExtensions(this DirectoryInfo dir, params string[] extensions) 
        => dir.EnumerateFiles().Where((f) => extensions.Contains(f.Extension));

}

Container to store file information

public class ItemDetails
{
    public string Directory { get; set; }
    public string FullName { get; set; }
    public string Name { get; set; }
    public DateTime LastWriteTime { get; set; }
    public string[] ItemArray => new[] {Name, LastWriteTime.ToString("MM/dd/yyyy")};
}

Code to get files by extensions

public class Operations
{
    public static List<ItemDetails> GetFiles(string path, params string[] extensions)
    {
        
        return new DirectoryInfo(path).GetFilesByExtensions(extensions).
            OrderBy(x => x.Name).
            Select(info => new ItemDetails()
            {
                Name = info.Name,
                Directory = info.DirectoryName, 
                FullName = info.FullName, 
                LastWriteTime = info.LastWriteTime
            }).
            ToList();
    }
}

Form code

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        
        Shown += OnShown;
    }

    private void OnShown(object sender, EventArgs e)
    {
        var path = "Your directory name goes here";
        if (!Directory.Exists(path))
        {
            return;
        }
        
        var items = Operations.GetFiles(path,".docx",".chm");

        DirectoryListView.BeginUpdate();

        try
        {
            
            foreach (var itemDetail in items)
            {
                DirectoryListView.Items.Add(new ListViewItem(itemDetail.ItemArray) {Tag = itemDetail});
            }
            
            DirectoryListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
            DirectoryListView.FocusedItem = DirectoryListView.Items[0];
            DirectoryListView.Items[0].Selected = true;
            
            ActiveControl = DirectoryListView;
        }
        finally
        {
            DirectoryListView.EndUpdate();
        }
        
        DirectoryListView.MouseDoubleClick += ListView1OnMouseDoubleClick;
    }

    private void ListView1OnMouseDoubleClick(object sender, MouseEventArgs e)
    {
        var current = (ItemDetails) DirectoryListView.SelectedItems[0].Tag;
        MessageBox.Show($"{current.FullName}\n{current.Directory}");
    }
}

Upvotes: 1

Related Questions