wpf获取目录路径

  AppDomain.CurrentDomain.BaseDirectory +文件名即可,简单吧?
  //获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。

  string str5=Application.StartupPath;

  //可获得当前执行的exe的文件名。

  string str1 =Process.GetCurrentProcess().MainModule.FileName;

  //获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。 备注 按照定义,如果该进程在本地或网络驱动器的根目录中启动,则此属性的值为驱动器名称后跟一个尾部反斜杠(如“C:\”)。如果该进程在子目录中启动,则此属性的值为不带尾部反斜杠的驱动器和子目录路径(如“C:\mySubDirectory”)。

  string str2=Environment.CurrentDirectory;

  //获取应用程序的当前工作目录。

  string str3=Directory.GetCurrentDirectory();

  //获取基目录,它由程序集冲突解决程序用来探测程序集。

  string str4=AppDomain.CurrentDomain.BaseDirectory;

  //获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。

  string str5=Application.StartupPath;

  //获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。

  string str6=Application.ExecutablePath;

  //获取或设置包含该应用程序的目录的名称。

  string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase

  

转载于:https://www.cnblogs.com/romanticcrystal/p/8397549.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用WPF读取XML文件的示例程序: MainWindow.xaml ```xml <Window x:Class="WpfXmlReader.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="XML Reader" Height="350" Width="525"> <Grid> <StackPanel> <TextBlock Text="XML File Path:"/> <TextBox x:Name="txtXmlFilePath"/> <Button Content="Read XML" Click="btnReadXml_Click"/> <ListBox x:Name="lstXmlData"/> </StackPanel> </Grid> </Window> ``` MainWindow.xaml.cs ```csharp using System.Windows; using System.Xml; namespace WpfXmlReader { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void btnReadXml_Click(object sender, RoutedEventArgs e) { lstXmlData.Items.Clear(); string xmlFilePath = txtXmlFilePath.Text.Trim(); if (!string.IsNullOrEmpty(xmlFilePath)) { try { // Load the XML file XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlFilePath); // Read the XML data XmlNodeList nodes = xmlDoc.SelectNodes("//book"); foreach (XmlNode node in nodes) { string title = node.SelectSingleNode("title").InnerText; string author = node.SelectSingleNode("author").InnerText; string price = node.SelectSingleNode("price").InnerText; lstXmlData.Items.Add(string.Format("Title: {0}\tAuthor: {1}\tPrice: {2}", title, author, price)); } } catch (XmlException ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } else { MessageBox.Show("Please enter XML file path.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); } } } } ``` 在此示例中,我们在窗口中添加了一个文本框和一个按钮,用于输入XML文件的路径并读取XML数据。我们还添加了一个列表框,用于显示读取的XML数据。 在“Read XML”按钮的单击事件处理程序中,我们首先清除列表框中的所有项。然后,我们从文本框中获取XML文件的路径,并使用XmlDocument类加载XML文件。接下来,我们选择所有的“book”节点,并使用SelectSingleNode方法读取每个节点的“title”、“author”和“price”元素的InnerText属性。最后,我们将读取的数据添加到列表框中。 如果加载XML文件时发生异常(例如,文件不存在或格式不正确),我们将通过MessageBox.Show方法显示错误消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值