Im saving images from a URL list but trying to duplicate the folder structure locally.
I parse the URL to give me the folder structure I want :
Example:
URL = www.site.com/images/folder1/folder2/image
My local base folder is mydocs/site/images
I split the url string up and am able to recursively create the proper folder structure using:
if (!Directory.Exists(finalLocalFolder))
{
DirectoryInfo di = Directory.CreateDirectory(finalLocalFolder);
}
Everything works great UNTIL I try and save the image to the folder using :
WebClient webClient = new WebClient();
webClient.DownloadFile(remoteUrl, finalLocalFolder);
In which case, I am told that access to that folder is denied.
"System.UnauthorizedAccessException: Access to the path
'mydocs\images\test\test1\test\2\3m' is denied."
So I am guessing that I need to create a step in the CREATEDIRECTORY area where I immediately grant access to that folder.
Is there an easy way to do this?
I can close this if you would like - but just to put up what my problem was... here it is:
In here: webClient.DownloadFile(remoteUrl, finalLocalFolder);
finalLocalFolder was ending up something like: "C:\mydocs\images\test\test2" when IT SHOULD HAVE BEEN "C:\mydocs\images\test\test2\theimagefilename.jpg"
Stupid moment. Sorry.
Related
i have an application which requires access permission to a file on remote server.
My app is in Server A, and the file i want to access is in Server B. These 2 servers are in the same domain.
I created a virtual directory in Server A for the directory in Server B. The name of virtual directory is FolderFromServerB and its path is \ServerB\Folder. I use a user for auth, and when i test the connection in IIS it says all is OK.
Also, when i put an anchor tag in a test file like below, i can access the file and the content is shown in the page:
Test file --> **This works**
But my problem is when i use code in order to if that file exists or not, it always returns with False. My code is like below:
FileInfo fi = new FileInfo(#"\FolderFromServerB/test.txt"); --> This doesn't work
Response.Write(fi.Exists); --> This always 'False'
I granted 'Full Control' permission to my user& NETWORK SERVICE & Everyone & Administratos in Server B but i didnt work neither.
How can i make it work?
It was working last week. I guess the server updated itself and some updates made that occur, but i couldn't find any workaround. Im so desperate now and i have to change all of my code and spend much time to make it work.
I found the workaround that is in web.config :
<identity impersonate="true" userName="{domain}\{username}" password="{password}"/>
I used File.Exist() for a few months, but then suddenly it was gone and didnt work, and i dont know why. But it is the solution above.
Your code does not work because the current execution folder of an ASP.Net application is not the folder of you application, but c:\windows\system32.
When you create the FileInfo object, you will try to read c:\windows\system32\FolderFromServerB\test.txt.
The <a href="FolderFromServerB/test.txt"> works because the link will be relative to the current page (it won't works if the page is in another directory).
If the file you are looking for is under your application directory, you can convert a virtual to a physical path using :
string actualFilePath = HttpContext.Current.Server.MapPath("~/FolderFromServerB/test.txt");
FileInfo fi = new FileInfo(actualFilePath);
How can I get the path of a file on my computer or on the local area network.
I would to show a window that allows me to browse the file system when I click a button, and I want to select a file and get the path to the file. How can I do this?
P.S. I'm not looking to upload the file; I just want to get the path.
The web application is running on the server, and you don't have access to the client file system at all. Can you imagine how much of a vulnerability that would be? I know I don't want the sites I visit inspecting my file system...
EDIT: Question is for a WinForms application
For a WinForms application, you can use the OpenFileDialog, and extract the path with something like this:
If you're looking for the file path:
string path = OpenFileDialog1.FileName; //output = c:\folder\file.txt
If you're looking for the directory path:
string path = Path.GetDirectoryName(OpenFileDialog1.FileName); //output = c:\folder
In general, the System.IO.Path class has a lot of useful features for retrieving and manipulating path information.
To do this in VB.NET use the FolderBrowserDialog.
Due to security restrictions browsers include for user safety, you can't manipulate the client file system directly. You can only use to let them pick a file, and even then it only sends the filename, not the whole path.
The FileSystem API in HTML5 allows for file manipulation, but only within a sandbox for your site specifically, not browsing across the network or other files on the client system.
Instead, provide your users easy steps on how they should use My Computer (or whatever equivalent on other OS's) to navigate to the file and copy & paste the path into a simple text input box.
Have you taken a look at the Path class from System.IO ?
im trying to create an xml file and then save it to a file location...
string xmlPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "cities.xml";
XDocument doc = new XDocument(
new XElement("Cities",
new XElement("City",
new XAttribute("id", gid),
new XElement("CityName", cityname))));
doc.Save(xmlPath);
the problem is that its not being saved to the location specified...
Try to use the System.IO.Path.Combine method to make sure you a) have the necessary backslash between the directory and the file name, and to b) make sure you don't have multiple of those:
string xmlPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
"cities.xml");
Also: maybe your user account doesn't have the permissions to write to that directory. Try using something like Isolated Storage or some other directory you're 100% sure that the user is allowed to write to.
The code looks fine and when I tested it locally it worked. Make sure that xmlPath points to a directory where the current user has write permissions. As a side note it would be better to use Path.Combine.
if you are using a windows application path will be pointing to the bin directory i think it is saving in bin directory
The best thing you can do is run this program through the debugger and check what the location is that is give in xmlpath variable, and also check if a normal local user has write permissions to this directory. There could be any number of problems with the folder or the path that is given. Without any other information however it is hard to give a more descript answer.
I have an ASP FileUpload control and I am uploading:
C:\Documents and Settings\abpa\Desktop\TTPublisher\apache-tomcat-6.0.26\webapps\ttpub\WEB-INF\classes\org\gtfs\tmp\GTFS_Rail\routes.txt
What is the C# code to grab this entire string using the code below:
var pathOfCsvFile = Server.MapPath(ImportRoutes.FileName);
var adapter = new GenericParsing.GenericParserAdapter(pathOfCsvFile);
DataTable data = adapter.GetDataTable();
I know that Server.MapPath needs to change.
UPDATE:
Using System.IO.Path.GetFullPath gave me the below output:
pathOfCsvFile = "C:\\Program Files\\Common Files\\Microsoft Shared\\DevServer\\10.0\\routes.txt"
You are mixing up client and server behavior, which is easy to do when you are testing locally. The issue you are having is that the FileUploadControl (and HTML file uploading in general) is specifically designed to not provide you with the full path to the file. That would be a privacy breach. What it is designed to give you is the binary data of the file that was uploaded itself. Specifically you should query the properties on FileUploadControl: FileBytes or FileContent.
Just to further clarify the issue, what would happen if the browser user was actually on a physically different machine from the web server (the usual case)? What good would the full path of the file on the client machine be to you on the server?
Server.MapPath will return the physical path to a file in or below the application root. If that path you list is outside the application root, Server.MapPath will not work.
You can map a virtual directory to a folder you want to use to hold file uploads, which you can then discover with Server.MapPath.
How do I search a sub directory in my ASP.Net web site?
I know if this was a regular Winform project, I could easily get it by the following:
Environment.CurrentDirectory
or
System.IO.Directory.GetCurrentDirectory()
then
string[] directories = System.IO.Path.Combine(Environment.CurrentDirectory, "Portfolio")
How do I build the path to a subfolder called Portfolio in ASP.Net?
I'm having a problem building everything from the http://??????????/Portfolio. How do I find the ?????? part?
I tried the code above but got a completely different directory...
I don't want to hard code everything before that last subfolder because it will be different on another server.
Calling Directory.GetCurrentDirectory() can be misleading when calling it from an ASP.NET app. The best way to do get around this is to use Server.MapPath() instead, which will allow you to map a web folder to its location on disk.
So for example, running this code:
string path = Server.MapPath("~/Portfolio");
will set 'path' equal to the location of that folder on disk.
If you're running this code from outside the context of a Page or control class, you won't be able to access the Server object directly. In this case, your code would look like this:
string path = HttpContext.Current.Server.MapPath("~/Portfolio");
If I understand you right, you can just access it with:
"/portfolio"
The system will concatenate "/portfolio" to the url, if, for example it is "http://site.com" you will get "http://site.com/portfolio"
If you want the physical path, you can use
server.mappath("/portfolio")
and it will return the physical directory associated (for example E:\websites\site\portfolio")
the following piece of lines gets you to the 'bin' directory
Assembly asm = Assembly.GetExecutingAssembly();
String strAppHome = Path.GetDirectoryName(asm.CodeBase).Replace("file:\\", "");