Read/write files in shared drive from web application hosted on iis - c#

We have two server in same domain. On one server (SERVER1) we have folder "C:\AppFiles\Production\Inbound" and shared AppFiles folder as a drive (AppFiles (O:)) to another server (SERVER2) and we are manually able to place files in shared drive (O:\Production\Inbound) on SERVER2. But we want to read/write files in shared drive (O:\Production\Inbound) from our mvc web application hosted on SERVER2.
We are getting below error in provided code when try to read/write in shared drive (O:\Production\
public void Writefile()
{
System.IO.File.WriteAllText(Path.Combine(#"O:\Production\Inbound", "testlog.txt"), "test test, text");
}
error:
Could not find a part of the path 'O:\Production\Inbound\testlog.txt'.
Please suggest how i can read/write files in shared drive from application hosted on iis.

Mapped drives are not supported on IIS, rather you can use UNC paths. Try changing the path to the following:
\\SERVER1\C$\AppFiles\Production\Inbound
If both the servers are on the same network and you can access the above link from server 2, the it should work.

Related

Accessing A shared network drive from a Microsoft Azure WebJob

I am trying to write a file to a location on a shared network drive on a remote server. When I run my code in my local environment, it works successfully and I can upload files. However, when running the code from an Azure WebJob, I get
Access to the path is denied
or
Access is denied
I am accessing the netwrok share using a method as posted here
and implementing as follows:
using (NetworkShareAccesser.Access(REMOTE_COMPUTER_NAME, DOMAIN, USER_NAME, PASSWORD))
{
File.WriteAllText(string.Format(#"{0}\TestFileName.csv", networkPath), sb.ToString());
}
As I say, if I run this on my local pc, it works and uploads the file. Something is different when running from an Azure WebJob. Is there some extra permission/ security required to access the shared drive from Azure?
Thanks in advance.

Access password protected shared network

How to read/write files in a shared network drive?
I am creating an application to upload files to the password protected shared network drive. When I run locally, the files are uploaded well. But, in the publish environment it throws:
Access to the Path '/shared network drive/' is denied
How to resolve this ?
Two ways:
1) Create a network drive connection to the remote machine (so it appears to your computer as "X:" for example) and save to that.
2) Use the full UNC path specification: //RemoteMachineName/SharedFolder/foldername

Common way of accessing files

I have projects which:
use deployed files (i.e. project can send mail using templates deployed with project)
local files system (i.e. for temp files)
and are running as:
web app on local IIS
windows service on local machine
web role on Azure
worker role on Azure
Now I have settings with path to deployed and local files and directories, but I have to set sometimes absolute paths (windows service), sometimes relatives (azure web role). Sometimes is hard to say, how path should be set.
Is there any way to have one way of setting path? Way, that would work in all above environments?
There's no one single way of accessing files in different environments. When your code runs in Web/Worker role, your application has access to specialized folder (local storage) and you read/write files there. When your code runs as Windows Service on local machine, you could grant access to any folder on that machine (and same goes for web app in local IIS).
I would recommend two options:
Abstract File System access and write concrete implementation per environment.
Use blob storage if it is possible.

Accessing an NFS share from a Windows service

I have a Windows service that can't access an NFS share on a UNIX box. I can't access the share with the UNC path, so I have to have it mounted.
I have a console application that can see the drive and runs fine, but when I deploy my Windows service, it can't see the drive.
I have the drive mounted as my local user as well as the administrator.
I've tried mounting a drive from my application by using "Process.Start(mBatchFilePath);", but that didn't work either.
I'm running as the local user (set as administrator).
Tried using a symbolic link
Going to try to set up an FTP to transfer the files
Does anyone have any suggestions on how I can make this work?
SOLVED: I used SSH.Net and transfer files with an SFTP server
I don't think that mapping the drive to a symbolic link is a real solution. The Problem is, that the service runs under a certain user account and thus it accesses all resources with the privileges of this account. Of course your IT admin is right, it's not a good idea to let the service run under the local administrator account.
You should create a new user account that is allowed to access the shared folder. Then set up the service that it runs under this account and the service should be able to access the network share. But remember, a network share that is mapped to a drive letter is only mapped for the current user. So the service (that runs under a different user account) simply doesn't have any drive with this letter, it is not mapped under its account.
So in your service you should always access the share with its full UNC path and not with a mapped drive letter.
Since we were connecting to a Solaris server from a windows box I needed to use SFTP to send files to the server.
I contemplated some sort of a folder sync, but it wasn't a route I wanted to go down.
I used the SSH.Net libraries.

upload file to the network mapped drive in c#

I am using c# asp.net mvc 3 and entity framework to upload the file to the network drive mapped as Z drive. I have following c# code for determining file path:
var path = Path.Combine(Z:/upload/catone/", fileName);
aries.SaveAs(path);
I am using window server 2008 and IIS 7. I have also check Security of upload folder of network drive and have acess full control for EVERYONE user. If I changed file path to local drive, it works fine. But it shows following error while uploading to network drive (Z:/).
Could not find a part of the path
'Z:\upload\catone\_2013011504265221N_todaily.wav'.
Your code is running under IIS and thus is run with access privileges of the IIS user account which may not have access to that network drive.
You can try runnning the app pool under your identity for instance, to rule this issue out, or give full access to the group IIS_IUSRS to that folder.
First of all save the file in local machine ,
than after use file.copy(Source, Destination, true) method to save file in network drive.
try that it is helpful for you

Categories