Read file from root folder using stream

There are two image files in my folder that I need to call in my program. I used:

AppDomain.curentDomain.baseDirectory + "Path and file name";

But this goes into my bin directory, which I don't want; I want to read a folder from the root directory, where the name of my folder as a resource, I saved my file there and called the image, so please, what is the code for this?

How can I read from the root directory in a windows form application?

+3
source share
5 answers

Usually you should install these items to copy to the bin folder. Right-click in the solution browser / navigator, select properties and set "Copy to output directory". hope it works

+2

Environment.CurrentDirectory?

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

.:) Environment.CurrentDirectory , .

+5

:

System.IO.Path.Combine(Environment.CurrentDirectory, "Path to File")

Environment.CurrentDirectory , . , Visual Studio .

// Read image1.jpg from application folder, into Image object
Image myImage = Image.FromFile(System.IO.Path.Combine(Environment.CurrentDirectory, "image1.jpg"));
+1
System.IO.Path.Combine(Application.StartupPath, @"..\..\YourFile.JPG")

, VS, bin\Debug.

string path = Path.Combine(Application.StartupPath, @"..\..\YourFile.JPG");
FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
Image image = Image.FromStream(stream);
stream.Close();

exe, , " ", , " : " " ": , bin\Debug . :

string path = Path.Combine(Application.StartupPath, "YourFile.JPG");

VS . .

+1

Server.MapPath("/path/to/file")

0

All Articles