I am trying to create directories and upload files to the server, but it won't give me access to. This is my code:
if (FileUploadControl.HasFile)
{
string path = "~/MSImages/";
string mappath = Server.MapPath(path);
if (FileUploadControl.PostedFile.ContentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
{
string extension = FileUploadControl.PostedFile.FileName;
extension = extension.Substring(extension.LastIndexOf('.'));
if (!Directory.Exists(mappath))
Directory.CreateDirectory(mappath);
string filename = imgext + Request.QueryString["id"];
FileUploadControl.SaveAs(mappath + filename + extension);
}
}
It works when done on my local computer, but not when on my host. How to fix this?
This is most likely a permission issue. Your IIS account user doesn't have the appropriate permissions to change the resource on the other machine.
You could change the IIS user to a low-permission domain user that is just granted access to that specific machine's share. That should fix your issue.
Related
I'm trying to use C# to create a file, and read files back to fill out a rich text block. right now my problem is in creating/writing to the file.
FileStream fs = File.Create(#".\\tmp\" + fileName);
This is where I'm trying to write to. .\tmp\ exists, but when trying to write it it errors, saying
.\tmp\filename access is denied
The probably is that the user that is running the application probably doesn't have access to write to that directory. The easiest way to test that would be to run your application as administrator you should have access to write to that directory then.
You might also want to consider writing to the current directory no matter what user who is running your application should at the very least have access to that directory
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
Probably you don't have access to the relative path.
To get your assembly directory:
private static string AssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
Then
FileStream fs = File.Create(Path.Combine(AssemblyDirectory, fileName));
You are using a relative path which leads to a location which you don't have access to.
A possible solution could be to:
Create a folder C:/data and make sure you have read and write rights to that folder
change the code to
string fileName = "file.txt";
FileStream fs = File.Create(#"C:/data/" + fileName);
This should create a file under C:/data with the filename "file.txt", assuming you have the correct read and write rights.
If you want a relative path to the current user's root directory, use:
string currentUserDirectory =
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
This happened to me . I had had an anti-virus blocking access to any file when the writing or reading process happening from a C# program. I have just deactivated the anti-virus and the code worked like magic !
my network path
//172.12.0.11/karomi snaps/dms/DH2304139/DH2304139_1_2_635023304446654623.jpg
now I tried with below but not worked
string imagePath = "//172.12.0.11/karomi snaps/dms/DH2304139/DH2304139_1_2_635023304446654623.jpg";
imagePath = "#"+"'" + imagePath +"'" ;
The UNC format for a network file path is:
string imagePath = #"\\172.12.0.11\karomi snaps\dms\DH2304139\DH2304139_1_2_635023304446654623.jpg";
I've added an # to avoid the first two \ being escaped. The space itself shouldn't cause an issue, assuming the account the app / site is running under has permission to connect to the share. You shouldn't need to wrap this in single quotes.
You can't do anything without the permission of the application provider, Because they hide the URL of the folder location with some IP or their customized URL.
I have been using a LocalDB.mdf file to build my application, but now need to use it in production and create an installable application.
After research, it seems the best place to store such a file is in the /Appdata/Local/ folder.
I intend to create a new LocalDB.mdf file in there if it doesnt already exist, or has been deleted.
I have a pre-made LocalDB.mdf file in my App Resources, which I wanted to copy into this /Appdata/Local/ folder on first run, but I am seeing an Access is denied error.
I can create a blank file ok to that folder.
Here is the code:
string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string dvAppDataFolder = appDataFolder + #"\MyApp";
AppDomain.CurrentDomain.SetData("DataDirectory", dvAppDataFolder);
if (!Directory.Exists(dvAppDataFolder))
Directory.CreateDirectory(dvAppDataFolder);
if (!File.Exists(dvAppDataFolder + "LocalDB.mdf"))
{
File.WriteAllBytes(dvAppDataFolder, LokiServer.Properties.Resources.newDB);
}
In addition, Am I going about this the right way?
This line
if (!File.Exists(dvAppDataFolder + "LocalDB.mdf"))
is probably wrong. Missing the backslash, better to use Path.Combine instead of a string concatenation.
Finally, you want to write to a file not to a folder
string fileName = Path.Combine(dvAppDataFolder,"LocalDB.mdf");
if (!File.Exists(fileName))
{
File.WriteAllBytes(fileName, LokiServer.Properties.Resources.newDB);
}
Are you doing it right? It depends. If your app data should be kept separated for each user of your target machine then you are right, but if you want your database to be available to all users of that machine then use
string appDataFolder = Environment.GetFolderPath
(Environment.SpecialFolder.CommonApplicationData);
from my aspx page i was tried to create a file. but it throws this error message if try to create the file from the root folder ( System.UnauthorizedAccessException: Access to the path '~/Image/User/mrrrrrfcom' is denied. )
Image1.ImageUrl = "~/Image/User/noneUserImage.jpg";
String folderPath = Path.Combine("~/Image/User/mrrrrrfcom", "mrrrrrfcom");
if (!Directory.Exists(folderPath))
Directory.CreateDirectory(folderPath);
but its working if i give it path like this
String folderPath = Path.Combine("G:/AA/BB/CC/DD/Image/User/", "mrrrrrfcom");
but u need to create file from the root access so that the project will work for other computers to.
Ensure that the user account under which the website is running (eg "iis apppool\DefaultAppPool" -- or whatever app pool -- in IIS7) has appropriate permission.
Manually I Have Mapped Network Drive Y:// To My System .Drive is having Manny Folders each Containg Single XMl File having Same as Folder .
Here I am Trying to Read Xml File From Network Location . But It is Giving Exception Directory Not Found . Below Code I am Using For that .
Fname = txtwbs.Text;
DirectoryInfo objDir = new DirectoryInfo("Y:\\");
\\Y:\\
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
_xmlpath = objDir + "\\" + Fname + "\\" + Fname + ".xml";
if (File.Exists(_xmlpath ))
{
reader(_xmlpath);
}
}
Here Fname is Folder Name Also Xml Name .Whatever User Will Enter the Name of File .
Grab this code: http://pastebin.com/RZnydz4Z
Then on the Application_Start of your global.asax put this:
protected void Application_Start(object sender, EventArgs e)
{
Utilities.Network.NetworkDrive nd = new Utilities.Network.NetworkDrive();
nd.MapNetworkDrive(#"\\server\path", "Z:", "myuser", "mypwd");
}
Then just use like a normal network drive, like this:
File.ReadAllText(#"Z:\myfile.txt");
You have this post tagged as both asp.net and asp-classic. From your code example, I'm guessing asp-classic doesn't apply.
If you are running in ASP.Net, the system wouldn't know about the mapped drive you've created. You should use the UNC path instead. If you aren't running the site with Windows Authentication, you'll also need to impersonate someone who has access to the share, as your anonymous user most likely will receive "Access Denied" errors.
Finally, I don't believe you need the DirectoryInfo call - use Path.Combine()
Ideally, you should use the UNC Path to access the files. \\server\share\path\to\file
Identity impersonate has done the trick for me
......
<system.web>
<identity impersonate="true" userName="yourdomain\yourusername"
password="yourpassword" />
......
......
Use this API. http://www.codeproject.com/Articles/6847/Map-Network-Drive-API
It the only way i have managed to do it in code.
Just modify the class, so it fits to you project.
Use a UNC path rather than a network drive and make sure the user running you application has permissions to access the folder and it's contents. "File not found" can mean wrong permissions or the path is just wrong.