简体   繁体   English

如何使用C#从Winform项目访问路径

[英]How to access path from winform project using c#

I am trying to access the folder from winform application using the below code but its give me this path 我正在尝试使用以下代码从winform应用程序访问文件夹,但它给了我这个路径

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 我正在使用此代码访问xml文件

 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 谢谢

Your problem is that when running from Visual Studio, your app is running from the bin directory. 您的问题是,从Visual Studio运行时,您的应用程序是从bin目录运行的。 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. 您可能希望将文件list.xml添加到输出目录,以便无论应用程序在哪里运行都可以使用它。 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). 在您的项目下添加一个新文件夹"xml" ,并向其中添加文件list.xml (使用右键单击上下文菜单中的“ 添加->现有项目 ”)。
  • 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. 现在,当您编译项目时,新文件夹和文件xml/list.xml将包含在/bin ,并且无论您在哪里运行应用程序,都应该能够访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM