0

I am trying to access the folder from winform application using the below code but its give me this path

D:\myproject\abc\bin\Debug\..\xml\list.xml

But my folder is is in this location

 D:\myproject\abc\xml\list.xml

I am using this code for access xml file

 protected void GetProcess()
 {
     var ps = Process.GetProcesses();
     pictureBox1.Visible = true;
     label2.Text = "Tracking Downloader";
     foreach (var p in ps)
     {
         try
         {
             Process[] proc = Process.GetProcessesByName(p.ProcessName);
             XDocument xdoc = XDocument.Load("..\\xml\\lst");
             var block = xdoc.Descendants("lst");
             foreach (var list in block)
             {
                 if (proc[0].MainModule.FileVersionInfo.FileDescription.Contains(list.Value) )
                 {
                     p.Kill();
                 }
             }
             //  listBox1.Items.Add(proc[0].MainModule.FileVersionInfo.FileDescription); 
         }
         catch
         {
             Console.WriteLine("Access Denied");
         }
     }
     //pictureBox1.Visible = false;
     label2.Visible = true;
     Console.ReadLine();
 }

Experts Please Help. Thanks

5
  • Why not "..\\..\\xml\\lst" then?
    – BartoszKP
    Commented Jan 3, 2014 at 12:21
  • hey i does not get ur point what you want to say ?? @BartoszKP Commented Jan 3, 2014 at 12:24
  • @mainrajput BartoszKP mentioned relative paths in C# Commented Jan 3, 2014 at 12:32
  • @ElliotTereschuk what is GetApplicationFolder() there???? Commented Jan 3, 2014 at 12:35
  • @mainrajput There are several ways in fact Commented Jan 3, 2014 at 12:43

1 Answer 1

3

Your problem is that when running from Visual Studio, your app is running from the bin directory. A compiled application will probably run from a different place.

You probably want to add the file list.xml to the output directory, so that it will be available from wherever the app runs. You can do this in two steps:

  • add a new folder "xml" under your project, and adding the file list.xml to it (use Add --> existing item from the right click context menu).
  • Right click the file, select Properties and change Copy to Output Directory to Copy always.

Now, when you compile your project, your new folder and file, xml/list.xml, will be included under /bin, and you should be able to access it from wherever you run your app.

7
  • How is it not working? Is the new folder and file being added to the bin/ directory? It should be. You might have to adjust your path a little, but if you can find out where it is being added (perhaps under bin/debug/?), figuring out the rest should be easy.
    – Kjartan
    Commented Jan 3, 2014 at 12:36
  • okk sorry my mistake i didnot add the folder in bin directory let me try Commented Jan 3, 2014 at 12:37
  • I add the xml folder in bin folder and when i run the application i get this error There are multiple root elements. Line 8, position 2. Commented Jan 3, 2014 at 12:41
  • Congratulations, it works! That error is related to the XML-file itself; apparently you have more than one root element (the outermost element of all your XML-content). That indicates that you can read the file anyway... :)
    – Kjartan
    Commented Jan 3, 2014 at 12:43
  • oh man! you are genius absolutely right but now its give me error that access id denied how can i resolve it :) Commented Jan 3, 2014 at 12:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.