c# - 如何在 C#.net 中读取 Windows 应用程序根目录中的文件?

标签 c# .net winforms filestream windows-applications

如果我使用 Application.StartupPath 或 AppDomain.CurrentDomain.BaseDirectory 它在 bin\debug 文件夹中搜索该文件 但我必须在 ma c# .net 项目中使用根目录“Resources\imagefile.png”中的文件夹!

Filestream fs;
fs = new Filestream(AppDomain.CurrentDomain.BaseDirectory + "folder\\imagefile.png");

那么应该如何编写代码来从我的根目录读取文件,尽管使用上面的代码或目录的完整路径是“@C:......”,甚至 Server.MapPath 是我们不能使用。

To get my application path ,but this gives something like C:\Projects\XYZ\ABC\bin\Debug

i don't want bin\Debug .Is there any way to achieve this ?

最佳答案

通过使用 Environment.CurrentDirectory

string yourpath = Environment.CurrentDirectory + @"\YourImage.jpg";
FileStream stream = new FileStream(yourpath, FileMode.Open, FileAccess.Read);
Image image = Image.FromStream(stream);
stream.Close(); 

关于c# - 如何在 C#.net 中读取 Windows 应用程序根目录中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12237104/

相关文章:

c# - 不允许在 IsolatedStorageFileStream 上对隔离存储中的 CreateFile 进行操作

.net - Argotic Syndication Framework 诉 System.ServiceModel.Syndication

c# - 使用时如何给SQL添加参数

c# - WCF 服务双重模拟?

.net - 表达式中未定义的函数 'Nz'

c# - 选中的列表框要求我选择一个项目两次

.net - 使用 "default proxy"时,用户名/密码从何而来?

c# - 如何在组合框的 DrawItem 事件中获取 DisplayMember 值? C#

c# - Signalr net client On 事件泛型类型参数

c# - 关于C#中Dispose()和析构函数的两个问题