繁体   English   中英

C#WPF-打印目录中所有格式为“ .xml”的文件-“找不到路径c#的一部分”

[英]C# WPF - Printing all files in directory that has “.xml” format - “could not find part of the path c#”

首先-我用谷歌搜索了这个问题,似乎没有相关的好消息。 同样,在您看来,这个问题可能是一个新手(我必须说,在目录中打印文件时我从未遇到过这个问题。对于C#中的WPF,我还是一个新手。

所以..我在打印“ .xml”格式的目录中的所有文件时遇到问题

这是我的代码,用于在目录中打印文件(我不是在谈论递归目录和文件打印):

    private void Load_ToolboxItems(string dirPath, string os, string version)
    {
        try
        {
            foreach (string command in Directory.GetFiles(dirPath, "*.xml"))
            {
                //load commands by OS compatibility
                MessageBox.Show(command);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

这是我的窗口加载事件:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Combined " + System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName), @"\data\Windows\xp\"));
        MessageBox.Show(System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName));
        Load_ToolboxItems(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName), @"\data\Windows\xp\"), "Windows", "XP");
    }

这些是我在运行程序时获得的输出:

第一个消息框-“ Combined \\ data \\ Windows \\ xp \\”

第二个消息框

C:\Users\Din\Din\Programming\Webs\Ended Projects\LogicalArm\Products\Visual Command Line\Visual_Command_Line-wpf_net3.5c_vs2010\Visual_Command_Line\bin\Release

第三个消息框-“找不到路径'C:\\ data \\ Windows \\ xp'的一部分。”

这是exe的起始位置:

C:\Users\Din\Din\Programming\Webs\Ended Projects\LogicalArm\Products\Visual Command Line\Visual_Command_Line-wpf_net3.5c_vs2010\Visual_Command_Line\bin\Release
foreach (String file in Directory.GetFiles(dirPath))
{
    if (Path.GetExtension(file) == ".xml")
        MessageBox.Show(file);
}

不知道您的根本问题是否是您没有得到所需的确切路径,但是以上内容应该从XML文件姿态列表中为您提供所需的内容。

从\\ data \\ windows \\ xp(第一个\\)中删除\\。 也要小心,因为您的路径越来越长。 不得超过260个字符。

Path.Combine()知道要使用的目录分隔符,因此,在使用它时,第二个参数不应以开头。

呵呵,

它总是发生在我身上..

我问了一个问题五分钟,经过长时间的搜索,我以某种方式自己找到了问题。

我的路径有问题-_-

感谢您尝试提供帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM