Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I made an app that uses a relative path to add nodes to a tree view.
The problem I have is that inside the treeview I have subfolders and files inside those subfolders. When I click a video in one of those subfolders I want to play it in wmp, but I get:
DirectoryNotFoundException was unhandled
Could not find a part of the path 'D:\Works\Nando\C#\Apps\Media Player\1\PMedia Player\PMedia Player\bin\Release\Release\Video\1\002. Configuring the Layout of Visu.mp4'.
//which is the full path of the selected file in the treeview

The code I am using is:
private void ListDirectory()
        {
            list.Nodes.Clear();
            var rootDirectoryInfo = new DirectoryInfo(Directory.GetCurrentDirectory().ToString());
            list.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo));
        }

        private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
        {
            var directoryNode = new TreeNode(directoryInfo.Name);
            foreach (var directory in directoryInfo.GetDirectories())
                directoryNode.Nodes.Add(CreateDirectoryNode(directory));
            foreach (var file in directoryInfo.GetFiles())
                directoryNode.Nodes.Add(new TreeNode(file.Name));            
            return directoryNode;
        }
private void list_AfterSelect(object sender, TreeViewEventArgs e)
        {
            WMPLib.IWMPPlaylist playlist = player.playlistCollection.newPlaylist("media_playlist");
            WMPLib.IWMPMedia media;
            //player.close()
            DirectoryInfo dir = new DirectoryInfo(list.SelectedNode.FullPath.ToString());
            if (list.SelectedNode.FullPath.Length!=0)
            {
            foreach (var file in dir.GetFiles())
            {                
                media = player.newMedia(Path.GetFullPath(Path.GetFileName(file.ToString())));
                playlist.appendItem(media);                
                path.Text=Path.GetFullPath(Path.GetFileName(file.ToString()));               
            }
            list.Visible = false;
            player.currentPlaylist = playlist;
            player.Ctlcontrols.play();            
            }else{
            MessageBox.Show("Directory not found!");
            }
        }

How can I fix this error?
Posted
Updated 20-Sep-15 12:56pm
v3

1 solution

Simple enough. NEVER use relative paths. ALWAYS build and use fully qualified paths for all file operations.
 
Share this answer
 
Comments
Member 10850253 21-Sep-15 14:53pm    
I changed the code where I use a relative path to:

var rootDirectoryInfo = new DirectoryInfo(Path.GetFullPath(Directory.GetCurrentDirectory()));

But I still get the error path not found in this line:

DirectoryInfo dir = new DirectoryInfo(Path.GetFullPath(Path.GetFileName(list.SelectedNode.Text)));
//Could not find part of the path in a foreach statement I use to get all the files using dir.GetFiles()
How can I fix this?
Thanks!
Dave Kreskowiak 21-Sep-15 17:22pm    
GetCurrentDirectory is not returning what you're think it does. It's probably the most useless of calls you can make. This is because what you think the "current directory" is probably isn't AND as users use your application and you supply things like any of the FileDialogs or whatnot, the current directory CHANGES.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900