简体   繁体   中英

How I can read a file in my Application folder?

Hi! I want to develop a windows service which I would install on a webserver. First I have to test my functions in a console application. I want to read a file in a Stream. In my Project Explorer I create a Folder with the name App_Data. In this I have three txt files and a pdf file.

using (StreamReader reader = new StreamReader(@"~/App_Data/PDFContent_de.txt",System.Text.Encoding.Default))
{
    string message = reader.ReadToEnd();

   //...
}

在此处输入图片说明

The folder you create is inside bin/debug

You can use "../../" to go back directories like

StreamReader reader = new StreamReader(@"../../App_Data/PDFContent_de.txt",System.Text.Encoding.Default)

or set the "Copy to Output Directory" property of the file to "Copy if newer" and just use

string path=@"App_Data/PDFContent_de.txt"

or

string path=Directory.GetCurrentDirectory()+@"/App_Data/PDFContent_de.txt";

or

string path=Path.GetFullPath("App_Data/PDFContent_de.txt");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM