如果我使用 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/