C# CreateSymbolicLink doesn't follow through share access - c#

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.

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.

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

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.

Check if file exists on remote server and various drive

We have an aspx page that needs to check if video files exist on our video server and display a link if the file does exist. However, our videos are not stored on the C drive, but on the D drive instead.
I have tried
System.IO.File.Exists(#"http://ourvideoserver/pcode/videofile_name.mp4") and
System.IO.File.Exists(#"\\ourvideoserver\\D:\\pcode\\videofile_name.mp4")
the last one was just taking a wild guess
And I cannot figure out how to check the files on a remote server on a different drive than C.
Could someone point me in the right direction on how to check in the D drive of the remote server
In UNC paths, drives are represented by a $. That is, D$. Try this:
System.IO.File.Exists(#"\\ourvideoserver\D$\pcode\videofile_name.mp4")
So, something like this should work (it does when I run it as a unit test with a change in the server name).
[TestMethod]
public void CheckUNCFileExists()
{
Assert.AreEqual(true, File.Exists("\\\\fileserver\\documents\\file.txt"));
Assert.AreEqual(true, File.Exists(#"\\fileserver\documents\file.txt"));
}
One thing that you might want to check is the name of the actual share, using an administrative command prompt on the file server (in my case it is the "documents" share):
C:\Windows\system32>net share
Share name Resource Remark
-------------------------------------------------------------------------------
C$ C:\ Default share
IPC$ Remote IPC
ADMIN$ C:\Windows Remote Admin
documents C:\documents
The command completed successfully.
If the path seems ok (and you can get to it with Windows Explorer), another thing to investigate is whether the application pool identity of your web application (probably you if you are debugging on your desktop using something like Visual Studio, or whatever your IIS admin configures on the web server side) has read access to the share (for windows sharing permissions) and read access (via NTFS permissions) on the folder/file (also, depending on how paranoid your admins are, you might also need "traverse" permissions on higher folders).
If you can't get to it with Windows Explorer, and you are using an administrative share (ex c$, d$, etc), you should re-share the folder with a different share name (since this will allow you to change permissions to make it readable by you/and the IIS application pool identity). If you are really bent on using the administrative share name, you'll have to modify permissions of the administrative share and may need to undertake something like this:
http://support.microsoft.com/kb/971277

How to map an ftp share folder to a local drive using C#?

How do I map an ftp share folder to a local drive using C#? Is there any class library available for this?
I need to achieve the same functionality as NetDrive(http://www.netdrive.net/) offers using FTP ?
Maybe you can leverage someone else's work and get a headstart. The makers of NetDrive for instance offer an SDK - not sure what that requires / costs, however. But it might be worth an inquiry, no?
And maybe, if you combine this approach to define a remote FTP site as a Windows network share, and then use this code in C# to mount a network share as a drive, you might get your job done :-)
It's a very tall order - if you actually want a local drive, e.g. X:\, to access an FTP site, you'd surely need to write a driver. Not an easy task.
If you want it to simply appear in Windows Explorer somewhere - you can use the shell extension as Marco's answer suggests. But don't expect to be able to treat it like a regular drive.
To create a virtual drive or a folder on existing drive and expose remote FTP server contents that way you need a filesystem driver. Or use can use our Callback File System (CBFS) product which lets you write the code in user-mode and includes a pre-created filesystem driver. CBFS includes a sample, SFTPDisk, that does exactly what you need, but with SFTP protocol (SFTP is not FTP but SSH File Transfer Protocol).
Note, that in FTP there's no function to upload a block to the middle of the existing file. This makes some file write operations trickier than with SFTP or local filesystem - you may need to cache the whole file and upload it asynchronously when it's closed by the client.
There's a project in C# to use an FTP folder as virtual drive -> http://amalgam.codeplex.com/ (it uses dokan).
There's a freeware program that does the same thing -> http://www.ferrobackup.com/ftpuse/

File.Copy from Unc path to (same server's) Unc path inquiry

Will data traffic go through the host application program or will be dealt remotely in scenarios where C#'s File.Copy is used:
File.Copy(#"\\SERVER13\LOL\ROFL.txt", #"\\SERVER13\ROFL.txt")
Cheers n thx!
First of all you have a small bug in the path of the destination file.
Second, there are no remote copy operation. There exist a remote move operation (rename, but with a destination in other directory) like MoveFile (see native API http://msdn.microsoft.com/en-us/library/aa365239%28VS.85%29.aspx).
UPDATED: Probably you came from unix and knows utility rcp, but it works with respect of remote shell service (rshd) and not with respect of direct file system features. You can also use PsExec utility from SysInternals (see http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx) to start some program on the remote computer, but all this is not a subject of programming.
It will go through the local application. The file system does not know what the application is going to do with the bytes it reads from the share, or where the bytes that are written to the share come from.
In addition, the application does not know (in the case of DFS) if the two shares are on the same machine.
If you want to let the server handle it, you have to remotely run a copy program.

Categories