从C#项目中的自定义文件夹获取文件 [英] Get files from custom folder inside C# project

查看:377
本文介绍了从C#项目中的自定义文件夹获取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次海报(和新手)。



我创建了一个C#winform应用程序。
我添加了一个Documents文件夹,其中我添加了5个PDF文件。



从我的Form1内,我添加了一个按钮在按钮点击事件中,我正在尝试从Documents文件夹中获取文件。



我已经google了,发现这样的东西:

  string [] arr = Directory.GetFiles(string path); 

但是我不想硬编码我的Documents文件夹的路径。
我想知道是否有一种方式(更动态)获取我的文档文件夹的路径。



我也找到了:

  string path1 = Path.GetDirectoryName(Application.ExecutablePath); 
string path2 = Path.GetDirectoryName(Assembly.GetExecutingAssembly()。Location);

但是他们总是带我到我的\bin\Debug文件夹。



我将尽全力帮助我!
谢谢!

解决方案

Environment.SpecialFolder 枚举您的意思?

  Environment.GetFolderPath或者我误解了这个问题?   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<



EDIT



我想我误读了,我的道歉。尝试这样:

  string documents = Path.Combine(
Path.GetDirectoryName(Application.ExecutablePath),
文件
);

这也假设您将documents文件夹中的项目作为资源所以可执行程序将能够看到它们。


First time poster (and newbie).

I've created a C# winform application. I've added a "Documents" folder in which I've added 5 PDF files.

From within my Form1, I've added a button and inside the button click event, I'm trying to get the files from that "Documents" folder.

I've googled around and found stuff like this:

string[] arr = Directory.GetFiles(string path);

But I do not wish to "hardcode" the path of my "Documents" folder. I'd like to know if there's a way (more dynamic) to obtain the path of my "Documents" folder.

I've also found these:

string path1 = Path.GetDirectoryName(Application.ExecutablePath);
string path2 = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

But they always bring me to my \bin\Debug folder.

I'll take all the help I can get! Thanks!

解决方案

Environment.SpecialFolder enumeration you mean?

Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

Or am I misreading the question?

EDIT

I guess I did misread, my apologies. Try this:

string documents = Path.Combine(
                     Path.GetDirectoryName(Application.ExecutablePath),
                     "Documents"
                   );

This is also assuming you're including the items from the "documents" folder as resources so the executable will be able to see them.

这篇关于从C#项目中的自定义文件夹获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆