簡體   English   中英

asp.net Web表單C#Streamreader找不到文本文件

[英]asp.net web form C# Streamreader can't find the text file

試圖制作一個愚蠢的Web表單,將一些小信息保留在“數據”文件夾中的文本文件中。 代碼是:

StreamReader sr = new StreamReader(Server.MapPath("data/FeatureList.txt"));
String FileText = sr.ReadToEnd().ToString();
sr.Close();

獲取異常:找不到文件'C:\\ Users \\ Tom \\ Documents \\ Visual Studio 2013 \\ WebSites \\ WebSite2 \\ data \\ FeatureList.txt'。

而且,自然地,該文件在指定的文件夾中。

也許我有一個權限錯誤,IIS無法讀取該文件夾或文件? 也許我需要告訴Visual Studio 2013有關此文件夾的信息? 一段時間以來,我沒有使用過其中一個asp.net程序。...好吧,幾年前,我制作了一個讀取文本文件並為顯示的照片添加標題的網頁。

您可以使用Path.Combine ,這是獲取有效路徑的好方法

StreamReader sr = new StreamReader(Path.Combine(Server.MapPath("~"), @"data/FeatureList.txt"));
String FileText = sr.ReadToEnd().ToString();
sr.Close();

嘗試,

string path = (string.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory,"\data\FeatureList.txt));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM