我有這個文件:C:\ Documents and Settings \ extryasam \ My Documents \ Visual Studio 2010 \ Projects \ FCR \ WebApplication4 \ config \ roles.txt我想將它導入到我的C#應用程序。如果我插入完整路徑,那麼可以,但我想要做類似於我們在網站上做的事情,那就是「\ config \ roles.txt」C#路徑問題
但是,下面的代碼不起作用。
這是我的代碼:
public string authenticate()
{
WindowsIdentity curIdentity = WindowsIdentity.GetCurrent();
WindowsPrincipal myPrincipal = new WindowsPrincipal(curIdentity);
//String role = "NT\\Internet Users";
//string filePath = Server.MapPath("config/roles.txt");
//string filePath = (@"~/WebApplication4/config/roles.txt");
//string filePath = Path.GetDirectoryName(@"\config\roles.txt");
string filePath = Path.GetPathRoot(@"/config/roles.txt");
string line;
string role = "";
if (File.Exists(filePath))
{
StreamReader file = null;
try
{
file = new StreamReader(filePath);
while ((line = file.ReadLine()) != null)
{
role = line;
}
}
finally
{
if (file != null)
{
file.Close();
}
}
}
if (!myPrincipal.IsInRole(@role))
{
return "401.aspx";
}
else
{
return "#";
}
}
它怎麼會出錯? – Jodrell
你是說你想從WinForms或Console應用程序訪問Web應用程序中的文件? –
這篇文章被標記爲ASP.NET,所以我認爲該文件在ASP.NET中訪問服務器端 –