Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 17th, 2006, 03:23 PM
Authorized User
 
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to load an xml file that is in same project?

Hey guys, I am new to winforms so please forgive me.
I am trying to write to an xml file that I created. The file is in the same project (in the root level of the project, not hidden away in sub folders).

However, it bombs out when it tries to find the xml file:

Here's the error:
{System.IO.FileNotFoundException: Could not find file 'S:\source\cs\sTopGun\CSharp\sBriefcase\BriefcaseS yncNotifier\bin\Debug\SyncSettings.xml

Here's my code so far:

                DataSet xmlDataSet = new DataSet();
                StreamReader myStreamReader = new StreamReader("SyncSettings.xml");
                xmlDataSet.ReadXml(myStreamReader);
                myStreamReader.Close();
I tried using StreamReader myStreamReader = new StreamReader(Application.StartupPath + "\\SyncSettings.xml");

but that didn't work either.

I checked and Application.StartupPath is returning the following:

S:\source\cs\sTopGun\CSharp\sBriefcase\BriefcaseSy ncNotifier\bin\Debug
It's going all the way to the Debug directory. I just need it to point to the following:

S:\source\cs\sTopGun\CSharp\sBriefcase\BriefcaseSy ncNotifier
Any ideas? Thanks!!!

 
Old November 28th, 2006, 02:50 PM
Authorized User
 
Join Date: Sep 2003
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

tryston02,

Not the most graceful solution, but here are two ways to accomplish this:

      string folder = Application.StartupPath;

      if (folder.ToLower().EndsWith(@"\debug"))
        folder = folder.Substring(0, folder.Length - @"\Debug".Length);

      if (folder.ToLower().EndsWith(@"\bin"))
        folder = folder.Substring(0, folder.Length - @"\bin".Length);

      string filepath = Path.Combine(folder, "SyncSettings.xml");

      //-----

      string anotherfilepath = Path.Combine(Application.StartupPath, @"..\..\SyncSettings.xml");

Cheers.

- Roger Nedel





Similar Threads
Thread Thread Starter Forum Replies Last Post
Access is denied" load a simple xml file nana XML 12 May 12th, 2010 07:01 AM
Can not load XML kolucoms XML 50 May 28th, 2008 05:25 AM
Parse/Load/Search xml file size near about 1 GB sandeep_akhare XML 4 September 19th, 2006 04:47 AM
Getting VB.NET to load & update XML file kyma XML 0 March 19th, 2005 04:58 PM
LOAD A XML FILE FROM JAVASCRIPT nen Javascript How-To 1 December 20th, 2004 11:03 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.