Is there a way to map a path from an external server? - c#

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?

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.

Related

C# - How to temporary stream file online to be accessed URL

I have been working on project recently where eventually I need two (or more) applications on different machines to be able to access the same file on one of them without uploading it to a server first.
Thus, the best idea I could get was to create something like a mini-temporary file server where the desired file path is streamed over the ip address and the other machine can access it via a URL like that "http://xxx.xxx.xxx.xxx/path/file.ext".
I have a good experience with C# but this approach... I never stepped into before, so any help is appreciated either in achieving this approach or any other method that can lead to allowing cross-internet access to a file on a machine.
Thanks in advance.
[Edit]
This operation has to be done without prior port forwarding, I don't know if this is possible, but I guess if it is not then I might need to do something like streaming to a php server first or something, again any help is appreciated.
Set up a virtual directory pointing to a local directory in IIS.
Application A writes file to local directory.
Application B reads
file from http.
Otherwise, you could use a network drive for both applications to read/write from.

Edit XML on Server via Service

Currently, I have a web based C# application (ServiceStack) that has an XML file it relies on to generate things client side. I no longer want to store this file on the client side. I need a way to use the repository I wrote to edit the file when it is stored on the server.
I have tried the following:
Finding the file location of XML when in the service. (it says that the service current directory is windows/system32, because it's running as a service. Makes sense now.)
Putting the file in the same project side by side. (same problem)
This already works:
Retrieves XML ( With hard coded path :c )
Deserialize XML
Add/Remove to XML as needed
Save
The key thing to this problem is it must live where the API lives. I don’t think that I am understanding the way this works very well, and I'd greatly appreciate some help.
Use System.Web.Hosting.HostingEnvironment.MapPath to find the path of the file relative to your application, assuming it's hosted via IIS.
In ServiceStack you'd normally use the Virtual File System to resolve files, e.g:
base.VirtualFileSources.GetFile("path/to/file.xml").ReadAllText()
Otherwise if you just want the path you can use IAppHost.MapProjectPath() available from v4.5.4 to resolve a file path relative to your WepApp consistently in all App Hosts, e.g:
HostContext.AppHost.MapProjectPath("~/path/to/file.xml")

Acessing files from a Server remotely

As the title says, I need to access files from a server remotely. After doing this, I need to show the files in a windows file/folder Dialog style.
I need this using Remoting.
It's not that simple since I have some problems:
I don't have access to the client UNC sharing;
I don't know where the \server\xpto is physically pointed;
The service at the server must use the physical path.
Also, the sharing must be enabled (not always is), I'd need to prompt username/password and I'd have the network path, not the physical.
So, it's like accessing a server in a data center using the client app.
I guess you can do it by a web application. Create a page on the server which can reach the local files on the server and displays a list of them on a grid. You can add some commands on the files as well. The page you created would act as a middleware to reflect the operations you want on the files.
hope it helps.

C# CreateSymbolicLink doesn't follow through share access

I'm not sure if this is just a C# problem or a windows limitation.
I have a server running my program which makes symbolic links in a shared folder. I can access through this link form the server with a normal disk path and the share path.
If I try to access the same share with an other pc I can't access the linked files. I do can delete rename... the links but I can't follow the links.
I already checked the acl of the link and the target files. Everyone should be able to access.
I use the following function:
[DllImport("kernel32.dll")]
static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, int dwFlags);
Maybe I just have to set some additional things? Or do I have to change some windows settings on the server?
Sorry if the solution is not a programming thing but I think here I have the best chances to get help with this.
Edit 1:
I have the share: C:\share
A link: C:\share\file.txt
to the destination: D:\file.txt
If I access the share from a different pc I can't access the data of file.txt.
This page from Tuxera has a good description of the reason
The similar concept of symbolic link is also available in Windows
Vista. The symbolic links can redirect to a file or a directory
defined by an absolute or a relative path. When defined on a remote
file system, they are processed on the local system, whereas the
directory junctions are processed on the file server, which makes a
difference when the target is not accessible by the file server.
The CreateSymbolicLink function will create a link that will be interpreted by the client. So the client computer (not the server) is trying to access a file with the name d:\file.txt which of course does not exist on the client.
For windows a Junction or Reparse Point will work on a server like your example. The easiest way to create one outside of code is to use the SysInternals Junction tool.

physical path in silverlight

I need to get physical path in silverlight. I'm using WCF service, I created one folder called 'Myfolder'. So I need to get the path of myfolder Please help me.
Silverlight doesn't allow direct access to the file system. However you can take advantage of Isolated Storage to read and write files on the client side. Here is a tutorial for that.
If you need access to a folder in the web application that is hosting your Silverlight app, use your service. Once you are in your OperationContract method, or even if you leverage the WebClient to make an AJAX style request, you can access the file system on the server but remember that is a different machine than your Silverilght client with the exception of when you do development (or browse your app on the server).

Categories