FileNotFoundException

Using an XML file in Windows Mobile

Page 1 of 1

3 Replies - 2236 Views - Last Post: 26 June 2009 - 10:59 AM

#1 jonofan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-March 09

FileNotFoundException

Posted 28 March 2009 - 08:09 PM

Hi All,

I am trying to develop a C# program for Window Mobile, it is an expenses program using an XML file as a database. I am using Visual Studio 2005 with the Windows Mobile SDK and have created the XML file through the IDE.

I believe I have the right file path but am getting an exception when trying to open the file.

Quote

Could not find file '\Program Files\DeviceApplication1\budget.xml'.


The XML file is obviously called budget.xml, I have tried hard coding the file path and also retrieving it.

class XMLHandler
	{
		private XmlDocument xdoc;
		private string filePath;
		private Hashtable expenses;

		public XMLHandler ()
		{
			xdoc = new XmlDocument();
			filePath = "\\Program Files\\DeviceApplication1\\budget.xml";
			// filePath = System.IO.Path.GetDirectoryName(
			//System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\budget.xml";
			expenses = new Hashtable();
		}

		public void openAndRead()
		{
			FileStream rfile = new FileStream(filePath, FileMode.Open);
			xdoc.Load(rfile);
			//get all the Expense nodes
			XmlNodeList list = xdoc.GetElementsByTagName("Expense");

			foreach (XmlElement currentElement in list)
			{
				string expenseName = currentElement.GetAttribute("Name");
				string expenseValue = currentElement.FirstChild.InnerText;
				expenses.Add(expenseName, expenseValue);
			}

		}

		public ArrayList getExpenses()
		{
			ArrayList exp = new ArrayList();

			foreach (DictionaryEntry entry in expenses)
			{
				exp.Add(entry.ToString());
			}

			return exp;
		} 
	}


I am getting the exception on this line: FileStream rfile = new FileStream(filePath, FileMode.Open);

I am testing this on the emulator which comes in the SDK, and have tried to locate the XML in the emulator but have not been able to, if anyone could guide me in the right direction that would be great.

Thanks
Jono

Is This A Good Question/Topic? 0
  • +

Replies To: FileNotFoundException

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: FileNotFoundException

Posted 28 March 2009 - 08:22 PM

Well the problem is pretty self explanatory, it can't find the file. This means two things... the file is not at the location you are telling it to look (so make sure you spelled the entire path correctly and for good measure also make sure it is the correct case and has no spaces) or there is something wrong with the path.

Try placing the file in a well known location that is short and readable like C:\ root folder and try to see if the application can find it using "C:\budget.xml".

Since we can't see your file system we don't know if you are specifying everything correctly so try it at a simple location first like C:\ and if it works there, then you know for sure there is something wrong with the location of the file or the path you are using to access it.

:)

This post has been edited by Martyr2: 28 March 2009 - 08:24 PM

Was This Post Helpful? 0
  • +
  • -

#3 jonofan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-March 09

Re: FileNotFoundException

Posted 30 March 2009 - 06:53 PM

Thanks for your response.

I have found if I use an actual cell phone rather than the emulator, and copy a file "budget.xml" into the directory of the program it all works fine.

So I guess that Visual Studio is not creating the xml file when running the program, is there some way I can force it to do so? The XML file was created in Visual Studio as part of my project, so I assumed it would be created.

I can easily get around this by creating one if there is not one already, but this is a bit of a pain for testing the program.
Was This Post Helpful? 0
  • +
  • -

#4 b.ihde  Icon User is offline

  • D.I.C for life
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,070
  • Joined: 29-September 08

Re: FileNotFoundException

Posted 26 June 2009 - 10:59 AM

Hi, i know it s late.. but maybe for others who wants to know..

Click on your .xml file in the soloution window, go to propertys and click at "Copy to Output Directory" and change it to -> Copy if never ;)

regards

Ben
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1