2011-08-03 12 views
1

私はこのファイルを持っています: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 "#"; 
     } 
    } 
+3

それはどうやって間違っていますか? – Jodrell

+0

WinFormsやConsoleアプリケーションからWebアプリケーションのファイルにアクセスしたいのですか? –

+0

投稿にはタグが付いたASP.NETがあるので、ファイルがASP.NETのサーバー側にアクセスしていると思います –

答えて

2

~/config/roles.txt - Server.MapPath()と組み合わせて、あなたは完全なパスを得ることができます。

[...] ASP.NET includes the Web application root operator (~), which you can use when specifying a path in server controls. ASP.NET resolves the ~ operator to the root of the current application. You can use the ~ operator in conjunction with folders to specify a path that is based on the current root. (see http://msdn.microsoft.com/en-us/library/ms178116.aspx)

だからあなたは次のことを試みることができる:

string filePath = System.Web.HttpContext.Current.Server.MapPath("~/config/roles.txt"); 
+0

私はC#でこれをやっている –

+0

C#は単なる言語です。あなたはC#でASP.NETアプリケーションを書くことができます...どのようなアプリケーションがありますか? ASP.NET(WebForms)、WinForms、コンソール、WPF ...?そして、あなたのアプリケーションはどこにありますか(ドキュメントに関連しています) –

+0

ASP.NET Webアプリケーションは、私が使用しているアプリケーションのタイプです –

0

あなたはあなたのファイルを押しF4を選択し、出力ディレクトリにコピーする選択する必要があります。その後、あなたはそれを使用することができるでしょう

1

Server.MapPathを使用すると、指定した相対パスまたは仮想パスをサーバー上の対応する物理ディレクトリにマップできます。

1

ローカルで作業しているので、そのファイルへの絶対パスを使用することができます。それは機能します。 しかし、roles.txtファイルを含むWebアプリケーションが一部のWebサーバーに展開され、ユーザーが別のマシンからこのファイルにアクセスしようとするとどうなりますか? あなたは、WindowsアプリケーションからWebサーバ上でホストされているアクセスファイルに以下のアプローチを使用することができます。

using (var stream = new WebClient().OpenRead("your_web_application_root_url/configs/roles.txt")) 
using (var reader = new StreamReader(stream)) 
{ 
    Console.WriteLine(reader.ReadToEnd()); 
} 

ネットワーク上でその共有のセキュリティ設定を警告したことは、かなり良いアイデアではありません。

+0

私はCVSに取り組んでいます。問題が発生したとき、私はパスを得るために必要なものがあることに気付きました。セキュリティは優先事項であり、あなたのアプローチは本当にオプションではありません。私はPathクラスを見ていましたが、私が念頭に置いているのは次のようなものです: "string filePath = Path.GetDirectoryName(@" config \\ roles.txt ");"しかしそれはまだ働いていない –