0

I am trying to share a folder between two websites that are on the same machine and configured with the same IIS managements screens.

I have tried the solutions in the other questions, but I could not get them to work.

I have tried the following:

  1. I created softlinks like this:

    mklink /d C:\Site2\Images C:\Site1\Images

I used a softlink rather than a hardlink because the rd command will just delete the softlink rather than deleting the files in the folder. Then I can redeploy my site without worrying about the images being deleting.

I suspect it is for security reasons that the folder cannot be shared between the sites.

  1. I also tried to make the folder like this:

    mklink /j C:\Site2\Images C:\Site1\Images

  2. I also tried using virtual directory configuration. That did not work either. I suspect it because I am trying to access the folders using VB code rather than by URL.

This is what the code looks like:

Public Shared Function Ck4Image(ByVal SubDir As String) As String
   Dim countfiles As String = ""
   Dim ckdir As String = HttpContext.Current.Server.MapPath("~/Images/") + Trim(SubDir) + "/"                     ' Set the path.
   Dim MyName = Dir(ckdir, vbDirectory)   ' Retrieve the first entry.

   If MyName = "" Then
      Ck4Image = ""
      Exit Function
   End If
   countfiles += SubDir & "|"
   For Each afile As FileInfo In New DirectoryInfo(ckdir).GetFiles("*.jpg")
      countfiles += afile.Name & "|"
   Next
   Ck4Image = countfiles
End Function

Here are my questions:

What is the tilde syntax in the MapPath function? It is not in the documentation that I read.

How can I achieve my goal of sharing the same folder between Site1 and Site2?

Note: I listed this on stack rather than server fault because I suspect it is a problem in my code that is preventing virtual directories from working.

Be Kind To New Users
  • 8,043
  • 10
  • 63
  • 102
  • Check out this: http://stackoverflow.com/questions/275781/server-mappath-server-mappath-server-mappath-server-mappath. Off the top of my head, I suspect that your use of ServerMapPath is part of, if not THE, issue. – Prescott Chartier Apr 04 '17 at 22:53

1 Answers1

0

I think you are over-complicating things a little here. You don't really need to use MapPath.

What I have done with our websites is to use appSettings in web.config to define the shared file path in multiple web applications.

<appSettings>
  <add key="IMAGE_DIR" value="C:\shared_images\" />
</appSettings>

Then you can easily reference path in your code:

Dim ckdir As String = AppSettings("IMAGE_DIR") + Trim(SubDir) + "/"
Robert
  • 446
  • 2
  • 14
  • Okay, I will try that, but doesn't the security of IIS prevent me from reading the file system outside of the deployed app? [Note: I am new to VB, IIS and just trying to help a friend who lost his IT guy who set this up] – Be Kind To New Users Apr 05 '17 at 00:22
  • You set the folders security to what YOU want. IUser is the account that is used when users anonymously hit your site. Give IUser whatever rights are necessary for you to do what you want with that folder. – Prescott Chartier Apr 05 '17 at 00:45
  • Surprisingly IIS does not prevent you from doing that. Not by default anyway. As long as IUser has access to that folder all you web app can access it. – Robert Apr 05 '17 at 01:11
  • if that is true then I find it strange that a softlink or a directory join is not working. There are about 100 spots in the code using the technique. I would like to find something that would work without changing code. In the meantime I am gearing up to make the change (change is easy, testing, not so much). – Be Kind To New Users Apr 07 '17 at 00:43