通过混合相对和绝对路径创建的问题 [英] Problem created by mixing relative and absolute paths
本文介绍了通过混合相对和绝对路径创建的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个使用相对路径将节点添加到树视图的应用程序。
我遇到的问题是在树视图中我有子文件夹和文件在这些子文件夹中。当我点击其中一个子文件夹中的视频时,我想在wmp中播放它,但我得到:
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?
推荐答案
简单足够。切勿使用相对路径。始终为所有文件操作构建和使用完全限定的路径。
Simple enough. NEVER use relative paths. ALWAYS build and use fully qualified paths for all file operations.
这篇关于通过混合相对和绝对路径创建的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文