0

When I run this code with console application project it works well, but When I run it in webpart it throws an exception "Access to the path is denied". I want to access directories informations with anonymous user from my sharepoint site.

I give full access (Read/Write) "wwwroot" folder for "Everyone", "ANONYMOUS LOGON", "IIS_IUSRS"

string VideosPath = @"\\10.2.10.2\inetpub\wwwroot\Videos";
string[] fileDirectories= 
  System.IO.Directory.GetDirectories(VideosPath, "*", SearchOption.AllDirectories);

Thanks for everone, I resolve problem using delegate

     SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                    {
                        using (SPWeb currentWeb = site.OpenWeb(SPContext.Current.Web.ID))
                        {
string[] fileDirectories= 
      System.IO.Directory.GetDirectories(VideosPath, "*", SearchOption.AllDirectories);

                         }
                    }
                }
              );
MikkaRin
  • 3,026
  • 19
  • 34
ygy59
  • 47
  • 1
  • 11
  • Is this path relative to your Site, your App or a physical Drive ? – H H Oct 17 '11 at 12:02
  • Maybe this will help >http://stackoverflow.com/questions/4986293/access-to-the-path-is-denied-when-using-directory-getfiles – user997287 Oct 17 '11 at 12:07
  • Maybe this will help >http://stackoverflow.com/questions/4986293/access-to-the-path-is-denied-when-using-directory-getfiles – user997287 Oct 17 '11 at 12:17
  • Did you definitely mean "\10.2.10.2\" and not "\\\\10.2.10.2\"? – Kieren Johnstone Oct 17 '11 at 11:56
  • No , I mean "\\10.2.10.2\.. " – ygy59 Oct 17 '11 at 11:59
  • 1
    It was very clear to me he was trying to access a network share, but the code included was missing a \. As such, me asking that question would most likely provide him the answer. However it looks more like a copy-paste error than a problem in the code, so in this case, adding another slash is not the answer. (Yes, I phrased a 'try adding a slash' as a question - I'm obviously alluding that that's the answer). E.g. "why this doesn't compile? : `if (1 + 1 = 2)` .. "did you mean `if (1 + 1 == 2)`?" is a valid answer :) – Kieren Johnstone Oct 17 '11 at 12:10
  • yes, answer is not missing slash , because there is no missing slash :) I did mistake when I copy and paste. – ygy59 Oct 17 '11 at 12:19

2 Answers2

1

Your @"\\10.2.10.2\... path would refer to the root of the current drive, that shouldn't be.

You need :

string VideosPath = Server.MapPath("~/10.2.10.2/..."); 
...

where ~/ is the root of your app and just / would be the root of the 'site'

H H
  • 263,252
  • 30
  • 330
  • 514
  • You might want to clarify: specifying a network share has nothing to do with the 'current drive'. \\10.2.10.2\ indicates a network share and so ~/10.2.10.2/ isn't what's intended – Kieren Johnstone Oct 17 '11 at 12:06
  • I have a sharepoint site in the server, I am trying access from my server to another server (/10.2.10.2/) , my code run on my server and it should connect another server and it should take video files. – ygy59 Oct 17 '11 at 12:53
1

The anonymous account of the web server is not the anonymous account on the remote machine.

To make it work, create new account on both machines with same name and password then have the application pool of the website run under this account. Maybe "only" having IIS running using this account will be enough, so try it first.

With this, grant that account permissions over the folder and all its parent folders.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208