Click here to Skip to main content
15,895,815 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 25-Aug-10 8:37am
v2
Comments
Sandesh M Patil 25-Aug-10 7:14am    
So in Browser control you are getting wrong path of xml?

Do you select the XML file in browser control?

1 solution

I assume you are using a OpenFileDialog control in a Windows Forms application and perhaps you are trying to retrieve the File's full path in the Form_Load() event. That is:

C#
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
}


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:

C#
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.
 
Share this answer
 
Comments
Dalek Dave 25-Aug-10 10:17am    
Comprehensive answer!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900