问题描述
我有一个XML文件,需要在我的应用程序中执行插入,更新和删除操作.要加载XML,我需要XML文件的路径,因此如果使用Path.GetFullPath("Programs.xml")
...,我将获得路径 E:\ myapp \ bin \ debug \ programs.xml .我可以使用此路径加载XML,但是当我使用浏览器控件时,如果浏览任何文件并尝试获取XML文件的路径,则它是Path.GetFullPath("Programs.xml")
返回浏览器控件的路径,但是我需要的XML路径.谁能帮助我,让我知道为什么会这样.我不确定为什么它采用文件浏览器路径而不是XML路径.
Hi,
I have an XML file where I need to perform insert, update and delete operations in my application. To load the XML, I need the path of the XML file, so if I use Path.GetFullPath("Programs.xml")
... I get the path E:\myapp\bin\debug\programs.xml. I am able to load the XML using this path, but when I am using a browser control and if I browse any file and try to get the path of the XML file, it is Path.GetFullPath("Programs.xml")
returning the path of the browser control, but the XML path I need. Can anyone help me and let me know why this is happening. I am not sure why it is taking filebrowser path instead of XML path.
推荐答案
private void Form1_Load(object sender, EventArgs e)
{
string filePath = Path.GetFullPath(openFileDialog1.FileName);
//This will return the path of the exe or dll of the application
}
如果您尝试在Form_Load事件中检索完整路径,系统将为您提供当前目录Path,即应用程序运行所在的dll或exe.可能就是您的情况.
您需要做的是,在使用OpenFileDialog控件选择文件后,您应该尝试检索文件的完整路径.步骤如下:
If you try to retrieve the full path in the Form_Load event, system will provide you the current directory Path, that is, the dll or exe from where the application is running. May be that''s what is happening in your case.
What you need to do is, you should try to retrieve the File''s full path after selecting the file using the OpenFileDialog control. Here are the steps:
private void Form1_Load(object sender, EventArgs e)
{
//No code here
}
private void button1_Click(object sender, EventArgs e)
{
// Show the file browser control in the Button click event (A button on the windows form)
// You need to add a Button Click event handler
openFileDialog1.ShowDialog();
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
//Retrieve the file's path when user selects the file. You need to add a FileOk click handler
//for the OpenFileDialog control
string filePath = Path.GetFullPath(openFileDialog1.FileName);
//Here you will get the full path of the selected file
}
希望这可以解决您的问题.
Hopefully, this will resolve your issue.
这篇关于Windows应用程序中XML文件的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!