protected void btnAutomaticUpload_Click(object sender, EventArgs e)
{
DirectoryInfo dir = new DirectoryInfo(#"\\space-bar\UZ\UZ Dept\Management\Data\directory_exists_here\");
bool atLeastOneSuccessfulUpload = false;
bool possibleFormatChange = false;
lblMessages.Text = string.Empty;
lblResults.Text = "<span style='font-size:large; font-weight:bold'><u>Results Log</u></span><br><br>";
//If our destination directory does not exist, exit
if (!dir.Exists)
{
lblResults.Text += "<span style='color:Red'>Expected directory does not exist!</span><br>" + dir.FullName;
return;
}
So I have been trying to run this code on a live server with a mapped network drive but it always seems to not be able to find the folder. Although when I am running this on Debug or LocalHost mode, it seems to be able to find the directory with no problem. Any idea as to why it's not working even with a UNC path coded? Does it have anything to do with permissions if any?
I am trying to build an automatic file upload parser.
If you are running under IIS it almost certainly doesn't have the rights to access a network resource. Check the Application Pool identity in IIS to determine which user your application is running under - its likely be a very restricted system account. This can be changed by altering the Application Pool settings in IIS Manager.
Related
I want to make a list of files from a provided path. actually I need to read all files from a given folder that is on some other server and not on my iis server.
I mapped the drive on my iis server to read it but its asking me for credentials when page loads. I dont want this. I have saved the credential on where the page has been uploaded on iis and mapped the drive.
string xrayPath = #"\\172.18.0.23\or\CARM\" + xrayPath;
List<FileInformation> directories = new List<FileInformation>();
List<FileInformation> lstFiles = new List<FileInformation>();
List<FileInformation> lstAllFiles = new List<FileInformation>();
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(xrayPath);
int fileId = 0;
PoplateFiles(ref lstFiles, ref directories, dir, 0, ref fileId);
foreach (var file in directories)
{
file.isDirectory = true;
lstAllFiles.Add(file);
}
foreach (var file in lstFiles)
{
file.isDirectory = false;
lstAllFiles.Add(file);
}
//////////
Please help me out, it works in visual studio when I test it but when I deploy it on iis on server, then it's asking me for credential and I am providing the credentials on browser, even then nothing happens. Please guide me.
I think I have resolved the issue myself. On the server where I mapped the drive, I created an application pool and selected the user that has access to the mapped drive in the identity field of application pool and set the Load User Profile to true.
then I assigned that application pool to my application virtual directory. Now, this code worked fine. Before that actually the identity user on application pool had no access to the other network path/mapped drive. So, I provided that user who had access to the path ie. in the security tab of the mapped folder. Now it runs under that user, so it doesnt ask for credentials. I hope it works....
The following code is c# from an aspx code-behind page. The problem is with the final system.io.file line which works fine on my local computer when developing, but when I run it on my IIS instance hosted on a server I maintain.... the text I wish is not appended to the file by this command. Thus far, I have made sure that the file has full read/write/create for the IIS user, network, local user, etc. I have also tried changing the owner to be the IIS_USRS user. No error is generated, though, I've never known how to debug the code-behind of a deployed site if it throws no error.
Thanks for any help.
namespace checkin
{
public partial class buttonclicked : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var backendclickedprocindex = Request.QueryString["clickedprocindex"];
string path = Server.MapPath(".");
string pathfile = path + "\\clickedprocindex.txt";
File.SetAttributes(path, FileAttributes.Normal);
Label1.Text = pathfile;
System.IO.File.AppendAllText(pathfile, backendclickedprocindex + ",");
}
}
}
What server OS?
In Server 2016, find the app pool used by your website and check the "Identity" of that app pool. If it reads "ApplicationPoolIdentity", you need to add that specific App Pool Identity to your file share. Go to your folder, right click, properties. Go to Security, Edit. Add the user "IIS AppPool\YOURAPPPOOLNAME" and give it the rights it needs.
I want to create a folder on my current user's desktop folder, however; I keep getting an access denied message. I have full write permissions under my profile in IIS.
string activeDir = #"C:\Users\dmm\Desktop\";
string newPath = System.IO.Path.Combine(activeDir, "mySubDir");
System.IO.Directory.CreateDirectory(newPath);
Any help would be appreciated.
Try using the built in objects to get the desktop path, and let .NET also handle the path building for the new folder. You will also want to check if the directory exists first.
string newFolder = "abcd1234";
string path = System.IO.Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
newFolder
);
if(!System.IO.Directory.Exists(path)) {
try {
System.IO.Directory.CreateDirectory(path);
} catch (IOException ie) {
Console.WriteLine("IO Error: " + ie.Message);
} catch (Exception e) {
Console.WriteLine("General Error: " + e.Message);
}
}
When you deploy an application on IIS by default it is executed with ApplicationPoolIdentity. Which is virtual user created and named as IIS AppPool\YourPoolName If this virtual user does not have write access to your desktop. You get that exception.
You have two options.
Give ApplicationPoolIdentity user write access to Desktop directory.
goto Desktop folder and add user IIS AppPool\YourPoolName with write access :
Change pool Identity to user which has write access to directory.
Go
IIS->Application Pools -> Your AppPool ->Advanced Settings -> Identity
->
Select Custom Account and click set button. and there you enter your windows user credentials.
I would recommend first option.
There are many to consider here, first of them being that your application is an ASP.NET application, and every current user will be different. If your application — just assume — runs correctly on your machine, it will never run on hosting environment because they do not grant write permissions to special folders and user accounts.
That said, you need to work in physical paths in order to create your directories.
var path = "C:\\Users\\afzaa\\Desktop\\";
var folder = Path.Combine(path, "folder");
Directory.CreateDirectory(folder);
The result of the above code is,
As you can see, the code properly works and has no issue at all in execution.
There are few things to note:
Your application has read/write permissions. Check IIS for that.
Your code can actually lookup the path you are trying to access. That applies to any folder inside Desktop too, a sub folder may have special permissions applied.
Do not do this, write the content online in your hosting domain. Users have different accounts and structures for folders and thus this will not work — Desktop path is different.
If you want to users to download the file, simply stream the file down and let them save it where they want to.
https://forums.asp.net/t/1807775.aspx?Create+e+New+Folder+Access+Denied+
https://answers.microsoft.com/en-us/windows/forum/windows_xp-files/unable-to-create-the-folder-new-folder-access-is/ac318218-a7b2-4ee2-b301-2ad91856050b
.NET MVC Access denied when trying to create Directory
If you ran your logic from an IIS application, you should use Server.MapPath:
System.IO.Directory.CreateDirectory(Server.MapPath(newPath));
My console app checks for the presence of a file on a network drive and logs a message when it does not exist. Today I deployed my app to a QA machine and File.Exists() has been returning false for files that do exist. I am running the app via windows task scheduler. When I ran it from the command line it seems to work fine. But either way I don't trust it now. Has anyone seen this behavior or have any insight?:
Using System.IO;
private static void Main()
{
var fileName = #"x:\folder\file1.txt"; //be a network share
If (!File.Exists(fileName)
{
LogMessage("File is not on disk.");
}
else
{
LogMessage("File is on disk.");
}
}
I suspect that the drives are not mapped when running from task scheduler. Try a UNC path
var fileName = #"\\server\share\folder\file1.txt";
Am getting error when you are going to upload the file on specified folder in the server. Here I am going to upload P6100083.jpg in storeimg folder. When I am going to upload I am getting the following error:
Access to the path 'C:\inetpub\vhosts\bookmygroups.com\httpdocs\storeimg\P6100083.jpg' is denied.
Can anyone help me... How to use permisiion and were to use...
My code is while uploading image
if (FileUpload1.HasFile)
{
float fileSize = FileUpload1.PostedFile.ContentLength;
float floatConverttoKB = fileSize / 1024;
float floatConverttoMB = floatConverttoKB / 1024;
string DirName = "storeimg";
string savepath = Server.MapPath(DirName + "/");
DirectoryInfo dir = new DirectoryInfo(savepath);
// string savepath = "C:\\Documents and Settings\\ssis3\\My Documents\\Visual Studio 2005\\WebSites\\finalbookgroups\\" + DirName + "\\";
if (fileSize < 4194304)
{
string filename = Server.HtmlEncode(FileUpload1.FileName);
string extension = System.IO.Path.GetExtension(filename).ToUpper();
if (extension.Equals(".jpg") || extension.Equals(".JPG") || extension.Equals(".JPEG") || extension.Equals(".GIF"))
{
savepath += filename;
FileUpload1.SaveAs(savepath);
}
}
}
Thanks in advance
I have no success making my upload or any write operation on filesystem work on IIS7.
Still getting the error: Access to the path is denied.
My AppPool is running under Network Service. I have granted all kinds of accounts Full Control (Network Service, Network, IIS_IUSR, Administrator, Users, Everyone), restarted the webservice several times, studied all IIS7 settings, googled for two hours and nothing works.
IIS7 and WS2008 s-u-c-k-s. Sorry for the term. Anybody can help?
I just wanted to add: I noticed that in the upload's destination folder's Properties there's this checkbox named "Read-only (Only applies to files in folder)" and it's checked. It cannot be unchecked, comes back checked after unchecking and clicking the OK button. Is that IIS7 guarding it?
Editing this message to add the SOLUTION: My admin has turned off the silly UAC "the security confirmation feature" on our server, restarted the machine and it works now. No "write" access rights for "Network Service" or any other IIS-used account was needed. When accessing the file system in a ASP.NET web application using the integrated authentication and having the impersonation set to true in its web.confing, the file system seems to be accessed by the authentified end-user's account, not by the Network Service account which the AppPool is running under. (Many people tell you to set Network Service permissions, but that is not true.) So you need to set the "write" permissions for your end-users (usually domain users: "DOMAIN\domain users") on your particular folder.
Oh yea, and the "Read-only (Only applies to files in folder)" checkbox mentioned above does not seem to have any effect. However Microsoft says "some programs might have problems writing to such folder and you should use command line statement "attrib -r -s" to get rid of the Read-Only attribute" -- but it won't work. It will stay there checked-grayed. But don't worry about that. Microsoft becomes more and more silly every day.
Indead, it's a server issue.
You need to verify if the user underlying your application pool has write access to the directory.
If you use IIS7, you have a new feature that helps you give custom write to this user and dun need to change the user.
Look at this link:
http://www.adopenstatic.com/cs/blogs/ken/archive/2008/01/29/15759.aspx
Hope this helps.
This is a server issue. Make sure you have the necessary rights to write files.
Btw, since you call ToUpper() on extension there's no reason to test for ".jpg".
If you are using Plesk Panel, go to file manager of Plesk Panel. List files and folders inside "httpdocs". Each file and folder has a lock icon at the very right. Click that of "storeimg" folder to change permissions. Click advenced button. Give full permission to these:
Plesk IIS WP User (IWPD_214(your_login_name))
Plesk IIS WP User (IWPD_214(your_login_name))
And click OK.
First you check the permission is enable or not if not then go to that folder which folder has to be use for containing files then right click on folder then there will be display folder properties then click on security there will be display multiple number of user which user have to be permit then click allow that all permission will be activated.
First, make sure your code runs fine locally (I assume that something you've already done).
Then deploy to your TEST or UAT environment. If you're having issue there, then this is a configuration issue. Make sure the service account under which your website's app pool is running has access to the folder.
Please make use of C# method Path.Combine() to build up your path and avoid issues with leading or trailing / and \.