Sunday, 15 September 2013

Asp physical and virtual path

Asp physical and virtual path

This is the first time I make an asp site. This code is working fine on my
pc but after deployment some feature which need a path is not working
anymore. I get no errors, simply does not work. I have tried many sort of
path combination but I am not getting it right. Moreover, I would like to
find a solution that makes the code working on both pc, during
development, and hosting server without having to change the code.
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode )
{
try
{
//check if directory exist and if not create directory
GridEditableItem editedItem = e.Item as GridEditableItem;
string recordIDcreateDir =
editedItem.GetDataKeyValue("TransazioneID").ToString();
string subPath = "Allegati\\" + recordIDcreateDir;
bool isExists =
System.IO.Directory.Exists(Server.MapPath(subPath));
if (!isExists)
System.IO.Directory.CreateDirectory(Server.MapPath(subPath));
//loop in the directory to search for files
GridEditableItem edit = (GridEditableItem)e.Item;
DirectoryInfo dir = new
DirectoryInfo(@"C:\Users\blablabla\Test1_managDoc\Test1_managDoc\Allegati\"
+ recordIDcreateDir);// path of the target folder where your
files are stored
DirectoryInfo[] subDirs = dir.GetDirectories();
FileInfo[] files = dir.GetFiles(); //Getting the files inside
the Directory
foreach (FileInfo fi in files) //To loop through all files for
setting each file as HyperLink
{
HyperLink lktest = new HyperLink(); //Add HyperLink Column
lktest.ID = "lnk" + Guid.NewGuid(); //Setting Unique IDs
lktest.Text = fi.Name.ToString(); //Get the File name
lktest.NavigateUrl = "#";
lktest.Attributes.Add("Onclick", "ViewCheck('" +
recordIDcreateDir + "/" + fi.Name + "')"); // Calling the
JS event
//Adding the HyperLink to EditForm
edit["columnAllegati"].Controls.Add(lktest);
edit["columnAllegati"].Controls.Add(new
LiteralControl("<br>"));
}
}
catch (Exception)
{ }
}
}
How should I change the physical path and the virtual path to get the code
working on both, pc and hosting server?

No comments:

Post a Comment