应用程序的代码路径 [英] code path of a application

查看:122
本文介绍了应用程序的代码路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我将xml文件保存在以下路径中
我需要动态获取此路径
其中示例xml是我的项目文件名
D:\ OfficeWork \ practice \ SampleXml \ SampleXml


我正在使用以下路径获取可执行文件D:\ OfficeWork \ practice \ SampleXml \ SampleXml \ bin \ Debug

hi all,

i kept my xml file in the following path
i need to get this path dynamically
where sample xml is my project file name
D:\OfficeWork\practice\SampleXml\SampleXml


i am getting executable file by using this path D:\OfficeWork\practice\SampleXml\SampleXml\bin\Debug

推荐答案

Dim path As String = Application.StartupPath
Dim found As Boolean = False
Do
    found = File.Exists(System.IO.Path.Combine(path, "sample.xml"))
    If Not found And path.Length > 3 Then
        path = System.IO.Path.GetDirectory(path)
    End If
Loop While Not found OrElse String.IsNullOrEmpty(path)

If found Then
    path = System.IO.Path.Combine(path, "sample.xml")
    ' do something with the path
End If


,我为简单起见建议您将SampleXml文件添加到项目中,然后在解决方案资源管理器中选择它.在属性窗口中,选择复制到输出目录",并确保将其设置为始终复制"或如果更新则复制".然后,它会在生成时显示在bin \ Debug目录中,您只需引用文件名即可,而不必计算文件的绝对路径.
I would suggest for simplicity that you add the SampleXml file to your project and then select it in your solution explorer. In the properties window select the "Copy to output directory" and make sure it is set to Copy always or Copy if newer. Then it will appear in the bin\Debug directory when you build and you can just reference the file name instead of having to calculate an absolute path for the file.


这篇关于应用程序的代码路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆