1

I have this file: C:\Documents and Settings\extryasam\My Documents\Visual Studio 2010\Projects\FCR\WebApplication4\config\roles.txt and I want to import it into my C# application. If I insert the full path it's ok, but I want to do something similar to what we do with websites, and that is "\config\roles.txt"

However with the below code, this is not working.

This is my code:

    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 "#";
        }
    }
Ryan S
  • 3,210
  • 17
  • 48
  • 79
  • You mean that you want to access file in web application from WinForms or Console application? – Yuriy Rozhovetskiy Aug 03 '11 at 08:37
  • The post is tagged ASP.NET so I think the file is accessed server side in ASP.NET – Stephan Bauer Aug 03 '11 at 08:44
  • I am developing an internal web based application, that file is being retrieved from a C# file, does this respond your query @OneHalfTrackMindMan – Ryan S Aug 03 '11 at 08:45
  • @Stephan Bauer - Sry, for that tag, since it's a .NET application thats why I added the tag, but the actual class from where the file is being retrieved is C#. – Ryan S Aug 03 '11 at 08:51
  • You may also like to look at the @ string literal to avoid those backslashes http://www.kowitz.net/archive/2007/03/06/the-c-string-literal – McArthey Aug 03 '11 at 08:52
  • @McArthey - Thanks, I was using them already – Ryan S Aug 03 '11 at 08:55

5 Answers5

2

In ASP.NET, you can use ~/config/roles.txt - in combination with Server.MapPath(), you can get the full path.

[...] 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)

So you could try the following:

string filePath = System.Web.HttpContext.Current.Server.MapPath("~/config/roles.txt");
Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58
  • C# is just the language. You can write ASP.NET-Applications in C#... What kind of application do you have? ASP.NET (WebForms), WinForms, Console, WPF...? And where is your application located (relative to the document?) – Stephan Bauer Aug 03 '11 at 09:21
  • ASP.NET Web Application is what the type of application I am using – Ryan S Aug 03 '11 at 09:24
  • Then you should be able to use the root operator - I added an example in my answer – Stephan Bauer Aug 03 '11 at 09:33
  • I cannot user Server.MapPath because this error pops up "The name 'Server' does not exist in the current context" – Ryan S Aug 03 '11 at 11:58
  • `System.Web.HttpContext.Current.Server...` – Stephan Bauer Aug 03 '11 at 12:02
  • Still getting this error: "Non-invocable member 'System.Web.HttpContext.Server' cannot be used like a method." – Ryan S Aug 03 '11 at 12:20
  • Sounds like you forgot to call `MapPath()`. Just take a look at my answer above. I tried it and it works fine for me. If you have a problem using `Server`, maybe you should open a new question since this is not discussion forum ;) – Stephan Bauer Aug 03 '11 at 12:38
  • it works mate, silly me, I spent so much time on it, that I was making silly mistakes, thanks loads – Ryan S Aug 03 '11 at 13:01
1

You can use Server.MapPath to map the specified relative or virtual path to the corresponding physical directory on the server.

McArthey
  • 1,614
  • 30
  • 62
1

Since you are working locally you can use absolute path to that file and it's will works. But what about situation when web application that contains roles.txt file will be deployed on some web server and user will try to access this file from another machine? You can use the approach below to access file hosted on a web server from a Windows application:

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

Be warned that share security settings over network is not quite good idea.

Yuriy Rozhovetskiy
  • 22,270
  • 4
  • 37
  • 68
  • I am working on CVS, that's when I encountered the problem, I noticed I need something which will get me the path. Security is a priority so your approach isn't really an option. I was looking at the Path class, what I have in mind is something like this: "string filePath = Path.GetDirectoryName(@"\\config\\roles.txt");" but it's still not working – Ryan S Aug 03 '11 at 08:53
0

You should select your file and press F4, and choose copy to output directory. Then you will be able to work with it

Yurii Hohan
  • 4,021
  • 4
  • 40
  • 54
0

You could try embedding the file as a resource in your project. Something like this: How to embed a text file in a .NET assembly?

Community
  • 1
  • 1
alun
  • 3,393
  • 21
  • 26