No worries Mike,
You have to amend the code a little bit. Because documents directory is
outside application, you have to know its physical location (in previous
example i used server.mappath to automatically determine virtual directory
phycical location). The only difference now, you must know both, full url to
the document folder exposed through IIS and physical location in ordrer to
get file list (remember asp.net account need read permission on this folder):
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateData();
}
}
private void PopulateData()
{
const string DocumentFolderPhysicalPath =
"c:\\temp\\physicaldocumentlocation\\";
const string DocumentFolderUrl =
"
http://localhost/virtualfoldernameyouexposed/"; // now it is hardcoded but
you could retreive it automatically
HyperLinkField hyperLinkField = new HyperLinkField();
hyperLinkField.DataTextField = "FullName";
hyperLinkField.DataNavigateUrlFields = new string[] { "Name" };
hyperLinkField.DataNavigateUrlFormatString = DocumentFolderUrl + "{0}";
gridView.Columns.Add(hyperLinkField);
gridView.DataSource = GetDocuments(DocumentFolderPhysicalPath);
gridView.DataBind();
}
private System.IO.FileInfo[] GetDocuments(string physicalPath)
{
System.IO.DirectoryInfo directory =
new System.IO.DirectoryInfo(physicalPath);
if (directory.Exists)
{
return directory.GetFiles();
}
else
{
throw new System.IO.DirectoryNotFoundException(physicalPath);
}
}
I don't have IIS at home so i cannot test it, but it should work.
Hope this helps
--
Milosz
:
Milosz,
Your example worked (unaltered). To do what I need to be able to do I mapped
a folder in IIS under the project as you suggested. But, I was unable to use
it as a Virtual Path (e.g., "~/MyNewFolder/", which points to a folder
outside the application directory) in the code. So, I tried to use the fully
qualified path. This worked but I had to comment out the Server.MapPath line
of code. This allowed me to see the files in that folder, but again they
weren't links.
Where am I going wrong with this?
Thanks,
- Mike
:
Hi Mike,
I prepared a working example for you. Importnat note: if your document
folder is not a part of the web application you have to map it under IIS (for
below example I simply created an empty folder called Documents in
application's root and put some tex files in it).
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateData();
}
}
private void PopulateData()
{
const string DocumentVirtualPath = "~/Documents/";
HyperLinkField hyperLinkField = new HyperLinkField();
hyperLinkField.DataTextField = "FullName";
hyperLinkField.DataNavigateUrlFields = new string[] { "Name" };
hyperLinkField.DataNavigateUrlFormatString =
ResolveUrl(DocumentVirtualPath) + "{0}";
gridView.Columns.Add(hyperLinkField);
gridView.DataSource = GetDocuments(DocumentVirtualPath);
gridView.DataBind();
}
private System.IO.FileInfo[] GetDocuments(string relativePath)
{
string physicalPath = Server.MapPath(relativePath);
System.IO.DirectoryInfo directory =
new System.IO.DirectoryInfo(physicalPath);
if (directory.Exists)
{
return directory.GetFiles();
}
else
{
throw new System.IO.DirectoryNotFoundException(physicalPath);
}
}
Hope this helps
--
Milosz
:
bpd,
Here is the latest version (I have modified this several times, with no
success):
FileInfo[] myList = StoresServ.stores.getDocList_New();
gvwFiles.DataSource = myList;
gvwFiles.Columns.Add(new HyperLinkField());
HyperLinkField hfld = (HyperLinkField)gvwFiles.Columns[0];
hfld.DataNavigateUrlFields = new string[] { "FullName" };
hfld.DataNavigateUrlFormatString = "{0}";
hfld.DataTextField = "name";
gvwFiles.DataBind();
Thanks for taking the time to look at this!
- Mike
:
On Jan 31, 1:31 pm, Mike Rand <
[email protected]>
wrote:
I am trying to get a list of files from a specified directory using the
System.IO namespace classes, and then use that list as the datasource for a
GridView. I have been able to do this successfully.
The problem is when I try to hook up a HyperLinkField it's not working.
Basically, what I want to end up with is to have the file name listed in the
column and have the link point to the full file path, for each file in the
list. I've been attempting to use the DataNavigateUrlFields property with no
success, it seems to recognize some fields but not others (for example, it
will recognize the field "name", but not "directory", with the former a
HyperLink will be created, but that later it will not, even though you can
see the directory in debug mode).
Any ideas, would be greatly appreciated!
Thanks,
- Mike
How are you hooking up the HL field? Can you show us your code?