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

Mo Patel
  • 2,321
  • 4
  • 22
  • 37
Azad Chouhan
  • 277
  • 1
  • 6
  • 18

1 Answers1

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.

Kjartan
  • 18,591
  • 15
  • 71
  • 96
  • 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 Jan 03 '14 at 12:36
  • okk sorry my mistake i didnot add the folder in bin directory let me try – Azad Chouhan Jan 03 '14 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. – Azad Chouhan Jan 03 '14 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 Jan 03 '14 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 :) – Azad Chouhan Jan 03 '14 at 12:46
  • That's strange.. perhaps the file is open in an editor, or another process at the same time as your application is trying to read it? Or perhaps if you've been debugging your app, some kind of lingering process is locking it? Not sure, this should work. If all else fails, I'd just try to restart VS and try again.. I'm sure you can figure it out somehow.. ;) – Kjartan Jan 03 '14 at 12:52
  • Actually, your stream might not be closed after reading the file the first time. Take a look at this question: http://stackoverflow.com/questions/277783/how-to-correctly-open-a-filestream-for-usage-with-an-xdocument – Kjartan Jan 03 '14 at 12:54