我有这个文件: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中访问服务器端 –