Go back to old file path after open file dialog box
-
I am having a problem for the following case: I have an xml file in the directory of the myproject.exe file of my project. as a result i m using relative path to open the xml file. I am openig the file using the following code:
XmlTextReader xtr = new XmlTextReader("center.xml");
I have a open file dialog box in that project and i read a MSAccess file using that open file dialog box. But after reading the MSAccess file when I try to read the xml file with the same code then exception raised and shows could not find file "path of the MSAccessfile\center.xml" So how can I get back to the directory whre the myproject.exe file located" -
I am having a problem for the following case: I have an xml file in the directory of the myproject.exe file of my project. as a result i m using relative path to open the xml file. I am openig the file using the following code:
XmlTextReader xtr = new XmlTextReader("center.xml");
I have a open file dialog box in that project and i read a MSAccess file using that open file dialog box. But after reading the MSAccess file when I try to read the xml file with the same code then exception raised and shows could not find file "path of the MSAccessfile\center.xml" So how can I get back to the directory whre the myproject.exe file located" -
I am having a problem for the following case: I have an xml file in the directory of the myproject.exe file of my project. as a result i m using relative path to open the xml file. I am openig the file using the following code:
XmlTextReader xtr = new XmlTextReader("center.xml");
I have a open file dialog box in that project and i read a MSAccess file using that open file dialog box. But after reading the MSAccess file when I try to read the xml file with the same code then exception raised and shows could not find file "path of the MSAccessfile\center.xml" So how can I get back to the directory whre the myproject.exe file located"Further to the pevious answer, using relative paths isn't a great idea. If you know for sure where a file is then use the path. The file
center.xml
should probably be in the runnign directory and not the application directory. Though normally the same, they can be different. The best to use isSystem.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
Panic, Chaos, Destruction. My work here is done.
-
You can get the .exe filename from : Process.MainModule.FileName Then get the directory from your process .exe and set the directory to OpenFileDialog.InitialDirectory.
I have done the following code and it solved my problem.
//using System.Diagnostics;
try
{
Process p = Process.GetCurrentProcess();
String location = p.MainModule.FileName;
int start = location.IndexOf(Process.GetCurrentProcess().ProcessName.ToString());
// MessageBox.Show(location+" "+start.ToString());
location = location.Remove(start);
// MessageBox.Show(location);
xml = location + "center.xml";
} -
I have done the following code and it solved my problem.
//using System.Diagnostics;
try
{
Process p = Process.GetCurrentProcess();
String location = p.MainModule.FileName;
int start = location.IndexOf(Process.GetCurrentProcess().ProcessName.ToString());
// MessageBox.Show(location+" "+start.ToString());
location = location.Remove(start);
// MessageBox.Show(location);
xml = location + "center.xml";
}