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:\\", "");
Related
I have a dll file that iv'e put on my IIS website. Is it possible to check the assembly version from this file. I know how to do this if the file was stored on my local machine but its not. this is the code i tried:
string FilePath = "";
FileVersionInfo VersionInfo = FileVersionInfo.GetVersionInfo(FilePath);
string productVersion = VersionInfo.FileVersion;
The problem i'm getting is that when i enter a website directory in my FilePath variable it errors and says you can't enter a website path. Is there another way to use a website as a path and not a directory from my c drive??
If it is possible please give me a starting point and some websites to learn this from or some sample code. Would be much appreciated.
URL <> FileSpec
Thanks for telling us what you've tried. Sounds like you are trying to reference the DLL by the URL, not the file spec. A path like this:
http://MyNetworkWebServer/assy.dll
goes thru IIS and HTTP which intentionally bars actually viewing the dll for many reasons - the dll will be coded to serve up some content maybe but certainly not allow you to examine the bits.
A path like this:
\\MyNetworkServer\IisFileShare\assy.dll
goes thru the filesystem / filesharing areas of the server and will allow you to call those functions if proper permissions and security settings are in place.
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.
What I am trying to do is read data from a CSV file located within a Windows application folder named "Attachments". In a web application you can get the path of the folder using
Server.MapPath(#"Attachments/sample.csv");
What is the equivalent call from a Windows application?
Below is my code.
string[] str = File.ReadAllLines(#"Attachment/sample.csv");
// create new datatable
DataTable dt = new DataTable();
// get the column header means first line
string[] temp = str[0].Split(';');
// creates columns of gridview as per the header name
foreach (string t in temp)
{
dt.Columns.Add(t, typeof(string));
}
Is the path relative to the executable? If so you can use Application.StartupPath to determine where the program was started, and then combine that with the relative file path to get the full path:
var fullPath = Path.Combine(Application.StartupPath, #"Attachment\sample.csv");
If your app is running as a service or uses ClickOnce deployment this won't work, though.
The Windows Application Folder could be identified by this enum
Environment.SpecialFolder.ProgramFiles
MSDN refs
To get a string containing the actual path and the full file name you write
string pathToFile = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
string fullFileName = Path.Combine(pathToFile, #"Attachment\sample.csv");
As noted by Cole Johnson in its comment, there is a problem if your hosting operating system is 64bit. In that case there are two application folders. One for 64bit apps and one for 32bit apps.
Using NET4, you could discover the current operating system bitness with the property
Environment.Is64BitOperatingSystem
and, if false, use a different enum
Environment.SpecialFolder.ProgramFilesX86
But after all this, I think you should change something in the architecture of your program.
In Web applications, usually you do not use folders outside the web-root.
But WinForms application has no such limitation and then you may install the CSV files in a different folder (MyDocuments comes to mind) and control the actual location on your hard disk via an option in the configuration file
REMEMBER: The Application Folder requires particular permission to write in (If you save your attachments there this is another reason to choose a different location than the Application Folder)
You can do this with So something like
Path.Combine(Application.StartupPath, #"Attachment\sample.csv");
Steve beat me to it but yeah, check intellisense for whatever folder you're looking for under :
string path = Environment.GetFolderPath(Environment.SpecialFolder.
properties
You'll find all kinds of system paths there.
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 ?
I have a C# .NET web project that I'm currently working on. What I'm trying to do is read some files that I dropped into a dir which is at the same level as fileReader.cs which is attempting to read them. On a normal desktop app the following would work:
DirectoryInfo di = new DirectoryInfo(./myDir);
However because it's a web project the execution context is different, and I don't know how to access these files?
Eventually fileReader will be called in an installation routine. I intend to override one of the Installer.cs' abstract methods so will this affect the execution context?
Use Server.MapPath to get the local path for the currently executing page.
Use the Server.MapPath method whichs maps the specified relative or virtual path to the corresponding physical directory on the server.
Server.MapPath("mydir/file.some")
This returns: C:\site\scripts\mydir\file.some
Script also can call the MapPath with full virtual path:
Server.MapPath("/scripts/mydir/file.some")
Here is the link to the MSDN documentation of MapPath.