Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read file in windows application root directory in C#.net?

If i use Application.StartupPath or AppDomain.CurrentDomain.BaseDirectory It Search in a bin\debug folder for the file but I have to use a folder from my root directory "Resources\imagefile.png" in ma c# .net project!

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

So how should write code to read file from my root directory in spite of using above code or a full path of the directory that is "@C:......." and even Server.MapPath is we cant use.

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 ?

like image 259
Kiran Solkar Avatar asked Sep 02 '12 15:09

Kiran Solkar


2 Answers

By Using the Environment.CurrentDirectory

string yourpath = Environment.CurrentDirectory + @"\YourImage.jpg";
FileStream stream = new FileStream(yourpath, FileMode.Open, FileAccess.Read);
Image image = Image.FromStream(stream);
stream.Close(); 
like image 140
MoreAsh Avatar answered Oct 10 '22 16:10

MoreAsh


Generally, you'd set those items to be copied into the bin folder. Right click in solution explorer/navigator, choose properties and set "Copy to output directory" to something different.

like image 35
spender Avatar answered Oct 10 '22 17:10

spender