0

Well, I've been working on my server with a method like the following:    

System.Web.HttpContext.Current.Server.MapPath("/path/something");

... and it's been working so far. Nevertheless, now I've encountered an issue. The thing is, I need to write a file that's located on an external server. Is there a method in C# to do this?

Rand Random
  • 7,300
  • 10
  • 40
  • 88
Jose Peres
  • 197
  • 1
  • 1
  • 18
  • 2
    If you're writing a path to another server, don't map the path, just write the file. It's unclear what relevance `MapPath` has regarding other servers. –  May 09 '18 at 19:16
  • how is that file shared with your webserver? If it's shared using windows file sharing (or a unix-based equivalent) then you can just address it by its UNC name e.g. `\\server\share\filename.txt`. Server.MapPath doesn't really come into it because that's for turning a path relative to the webserver into a path on disk, but a folder shared from another server won't be part of your webserver. You just need to know the absolute UNC path to the file, and of course your application needs the relevant permissions to write to it. – ADyson May 09 '18 at 19:17
  • You can map the external server location to a network drive. For instance, you could map `\\server\myfiles` to `M:\\`. Another option would be to create a virtual Directory somewhere under the website in IIS that points to the network path. – JuanR May 09 '18 at 19:31
  • All your answers were also great. Thanks. – Jose Peres May 10 '18 at 16:28

1 Answers1

1

Server.mappath is designed to convert web based paths to local paths based on the location of the webroot, and is not strictly speaking relevant when trying to access external resources.

The only exception being when a remote file location is used as part of a websites file system. E.G. If your website has a folder in it called "/remotefiles/", and within IIS you have mapped this folder to a remote network path. MapPath would work as normal and you will retrieve a full UNC path.

If the server you want to access is on the same network as your web server but not referenced as part of your webroot, then you should look at directly referencing the location in question using a full UNC path. E.G. "\\ExternalServer\CDrive\SomeStuff\".

If the server is remotely accessed over a wider network, then you will need to look into another form of access.

Tony Cheetham
  • 877
  • 7
  • 18
  • Other answers were given before this one, but yours was the clearer, the best one. Thanks for this, it worked as expected. – Jose Peres May 10 '18 at 16:28