简体   繁体   English

Windows窗体C#中的文件夹

[英]Folder in windows form c#

This is my first windows form application. 这是我的第一个Windows窗体应用程序。

I need to work with folders that I have created in my project and I need to access the Data folder where I put .txt files. 我需要使用在项目中创建的文件夹,并且需要访问放置.txt文件的Data文件夹。

I try : 我尝试:

string fileName = @"Data\TextFile1.txt";
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);

but i keep receiving this error : 但我一直收到此错误:

impossible find part of path. 不可能找到路径的一部分。

How can I combine the folder's path with file's name so when I release the project all works well? 如何将文件夹的路径与文件名结合在一起,以便在发布项目时一切正常?

This is what I do in an asp.net application: 这是我在asp.net应用程序中所做的:

Path.Combine(HttpRuntime.AppDomainAppPath, "Folder/FileName.txt");

in C# .NET you can easy use the Environment Properties when working with Forms. 在C#.NET中,使用窗体时可以轻松使用环境属性。

If you want eg the Appdata Path do this: 如果您想要例如Appdata路径,请执行以下操作:

string MyPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\" + MyFileName;

But please get sure that the File/Path Exists! 但是请确保文件/路径存在! For this you can use File .Exists -Function: 为此,您可以使用File .Exists -Function:

if(File.Exists(MyPath))
{
//Do Something
}

(For File -Class you need the System.IO namespace!) (对于File -Class,您需要System.IO命名空间!)

EDIT: 编辑:

Example: 例:

  string MyPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\MyConfig.txt";

if(File.Exists(MyPath))
{
 MessageBox.Show($"{MyPath} exists!");
}

Hope that helped ;) 希望有帮助;)

If I understand correctly, you are trying to read some file you have added to your solution in Visual Studio. 如果我理解正确,则您正在尝试读取一些已添加到Visual Studio解决方案中的文件。 First, to have those files "deployed", click on them in the Solution Explorer, go to the Properties tool and have a look at the "Copy to Output Directory". 首先,要“部署”这些文件,请在解决方案资源管理器中单击它们,转到“属性”工具,然后查看“复制到输出目录”。 Default is "Do not copy". 默认值为“不复制”。 Change that setting to "Copy always" or "Copy if newer". 将该设置更改为“始终复制”或“如果更新则复制”。 Now your files will be copied to the output directory when you build the solution. 现在,在构建解决方案时,您的文件将被复制到输出目录。 Then, to get the current assembly path at runtime, have a look at: Getting the path of the current assembly 然后,要在运行时获取当前程序集路径,请看一下: 获取当前程序集的路径

string path = (new System.Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath;

from the assembly path, you can get the containing folder's path, then you can use Path.Combine() to get your desired file path. 从程序集路径中,可以获取包含文件夹的路径,然后可以使用Path.Combine()获得所需的文件路径。

I would not create any path inside the application if possible. 如果可能的话,我不会在应用程序内部创建任何路径。 The NET framework provides the application configuration file for this kind of problems. NET框架为此类问题提供了应用程序配置文件。 You should simply add this section to your configuration file (app.config when developing, then application.exe.config after compilation) 您只需要将此部分添加到配置文件中(开发时为app.config,编译后为application.exe.config)

<configuration>
  <appSettings>
    <add key="mydatafolder" value="e:\whateverpathyoulike"></add>
  </appSettings>
</configuration>

Then at runtime read that value from your code 然后在运行时从您的代码中读取该值

string path = ConfigurationManager.AppSettings["mydatafolder"];
path = Path.Combine(path, fileName);

This is easily modifiable both automatically or manually to adapt to any environment you find on the destination computers 这很容易自动或手动修改,以适应您在目标计算机上找到的任何环境

Your data files need to be available in output folder along with you application .exe file. 您的数据文件需要与应用程序.exe文件一起在输出文件夹中可用。 to do that: 要做到这一点:

  1. Open properties of each file in Data folder. 在“ Data文件夹中打开每个文件的属性。
  2. Select Copy Always against Copy to output directory 选择Copy Always反对Copy to output directory
  3. Then build application 然后建立应用

This will copy Data folder along with all files in Bin\\Debug folder and will work with your existing code. 这将复制Data文件夹以及Bin\\Debug文件夹中的所有文件,并将与您现有的代码一起使用。

在此处输入图片说明

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

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