1

I have one .net windows application I'm trying to read .xml file from c# windows application.

but i'm getting error :

Could not find a part of the path 'c:\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\~\Files\test.xml'.

but my file is in Files folder which is in application WindowsFormsApplication1 only not in \bin\Debug

then why it is searching into \bin\Debug ?

code in form.cs

DataSet dsAuthors = new DataSet("authors");
            string filePath = @"~/Files/test.xml";

            dsAuthors.ReadXml(filePath);

also please tell me is there any way to use Server.MapPath like we do it in web application.

I tried other solution like :

string appPath = Path.GetDirectoryName(Application.ExecutablePath);

            System.IO.DirectoryInfo directoryInfo = System.IO.Directory.GetParent(appPath);
            System.IO.DirectoryInfo directoryInfo2 = System.IO.Directory.GetParent(directoryInfo.FullName);

            string path = directoryInfo2.FullName + @"\Files";

but getting error : Access to the path is denied.

2
  • \bin\Debug is the default path when you start your application in Debug mode. Set the starting path in your project properties. Commented Jun 26, 2012 at 6:43
  • 1
    you can change the settings of test.xml - file in your solution to be copied into the debug-folder as well Commented Jun 26, 2012 at 6:45

2 Answers 2

2

Try:

        string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.Combine("Files", "test.xml"));// @"~/Files/test.xml";

If it doesn't work make sure that the test.xml has "Copy to output directory" property set to "Copy always".

Sign up to request clarification or add additional context in comments.

Comments

0

Use Server.MapPath

You are using a relative url. This will not work with ReadXml. It needs the absolute path on your disk.

Server.MapPath will take your virtual path and give you its absolute path.

var fullPath = Server.MapPath("~/Files/test.xml");
dsAuthors.ReadXml(fullPath);

Note: Server.MapPath doesn't verify if the path actually exists.

4 Comments

server.mappath is not available in WinForms apps
The concept of a ~ in the path doesn't exist in winforms
server.mappath is not available in WinForms apps - any other alternative for this?
Don't use a ~ . Your exe will always run in your output folder. Specify your path from there. A better approach would be to copy this test.xml to output. Assuming directory structures that you are not creating is asking for trouble.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.