problems in running app at win startup
-
Hi there I am having problems with loading my app at windows startup. When i load my app by clicking on it, it works fine but when i load it on windows startup, by adding a registry key in HKLM\Software\Current Machine\Microsoft\Windows\Current Version\Run it gives errors, the errors are of my files that i load when my app starts. i have an xml file that i load when my app starts that xml file has the location of my database file. for now i store my db file where my app's executable is. The code for my class :
using System;
using System.Xml;
using System.Xml.XPath;namespace Scheduler
{
public class SettingsXml
{
XmlDocument doc = new XmlDocument();public SettingsXml() { doc.Load("scheduler.xml"); // this means that this file // is where the app's .exe is } public string DBPath() { string xstr = "/Scheduler/Configurations/DbPath"; XmlNode xnode = doc.SelectSingleNode(xstr); return xnode.InnerText; }
}
}when the app loads at win startup it looks for my "scheduler.xml" file in "c:\scheduler.xml" rather then the place i have told it to! I fixed the "scheduler.xml" file's location problem by doing this:
doc.Load(System.Windows.Forms.Application.StartupPath + "/scheduler.xml");
It worked. But still in my xml file the path of my db file is "scheduler.mdb" now what do i do for this? cause when i distribute my app it has to have some default place to place my db file. What is the problem here? VisionTec
-
Hi there I am having problems with loading my app at windows startup. When i load my app by clicking on it, it works fine but when i load it on windows startup, by adding a registry key in HKLM\Software\Current Machine\Microsoft\Windows\Current Version\Run it gives errors, the errors are of my files that i load when my app starts. i have an xml file that i load when my app starts that xml file has the location of my database file. for now i store my db file where my app's executable is. The code for my class :
using System;
using System.Xml;
using System.Xml.XPath;namespace Scheduler
{
public class SettingsXml
{
XmlDocument doc = new XmlDocument();public SettingsXml() { doc.Load("scheduler.xml"); // this means that this file // is where the app's .exe is } public string DBPath() { string xstr = "/Scheduler/Configurations/DbPath"; XmlNode xnode = doc.SelectSingleNode(xstr); return xnode.InnerText; }
}
}when the app loads at win startup it looks for my "scheduler.xml" file in "c:\scheduler.xml" rather then the place i have told it to! I fixed the "scheduler.xml" file's location problem by doing this:
doc.Load(System.Windows.Forms.Application.StartupPath + "/scheduler.xml");
It worked. But still in my xml file the path of my db file is "scheduler.mdb" now what do i do for this? cause when i distribute my app it has to have some default place to place my db file. What is the problem here? VisionTec
visiontec wrote: doc.Load("scheduler.xml"); // this means that this file // is where the app's .exe is Unfortunately, no it doesn't. The file is tried in the current directory. The only reason why that works in your testing is because you launched the EXE from the folder by double clicking on it, thereby the current directory is the folder you double-clicked in. When you launched the EXE from the Run key, the current directory is C:\, or one of the Windows directories, depending on what's going on. Your app is being launched with the full path to the EXE, but that doesn't make it the current directory. To get around this problem, best-practice would be to code full path names into your app, but NOT hard-coded paths. Use configurable paths like:
StreamWriter sw = New StreamWriter(datapath + "\scheduler.txt");
You could also set the current directory when your app starts by using
Environment.CurrentDirectory = datapath;
But, best practice is to assume nothing! Don't assume that because you set the CurrentDirectory, that it is going to stay there! EDIT: Whoops! I supplied the code examples in VB, not C#. Rewrote for C# examples. RageInTheMachine9532