PDA

View Full Version : ASP.NET C# List files on server outside application folder


Uncork
08-11-2009, 01:08 PM
Using C# codebehind in VS2008 I am trying to list and ultimately read/write files on my server (running mono), but located in a different folder to the root folder of my application

eg:
server_root/data_folder/data
vs
server_root/application_folder/application.aspx



Here is the code snippet I am using, which works when debugging on my local (windows) server but only shows files in the application folder (or ???? if I exclude Server.MapPath) when running on my remote server running mono


protected void Page_Load(object sender, EventArgs e)
{
string s = "";
string sFolder = "";
string[] MyDirList;
DirectoryInfo MyDir;
ArrayList CellarList = new ArrayList();

if (Request.IsLocal)
{
sFolder = "c:/";
MyDir = new DirectoryInfo(sFolder);
}
else
{
sFolder = "/";
MyDir = new DirectoryInfo(Server.MapPath(sFolder));
}
FileInfo[] MyFiles = MyDir.GetFiles("*.*");
foreach (FileInfo MyFile in MyFiles)
{
s = MyFile.Name;
CellarList.Add(new CellarFileData(s, s));
}

Repeater1.DataSource = CellarList;
Repeater1.DataBind();


(I am using a repeater to simply display the files till I work out the correct syntax)

Thanks for your help

HostingASPNet
08-12-2009, 03:28 PM
Hello,

You should run your ASP.NET application in Full Trust Level - http://msdn.microsoft.com/en-us/library/wyts434y.aspx

Regards

Uncork
08-13-2009, 12:57 AM
Thanks for your response


By trial and error I found that if I changed ...
MyDir = new DirectoryInfo(Server.MapPath(sFolder));
to
MyDir = new DirectoryInfo(sFolder);
... and set sFolder to the full server path from "/home/......" then I can read folders and files to a point.... but I cannot quite get to the folder I need to. I do not want to publish the exact folder on the forum, but have checked its permissions and it has public read/write access.


I have also added the following to <System.web> in my config file...
<securityPolicy>
<trustLevel name="Full" policyFile="internal"/>
</securityPolicy>

... but still no difference

Any other ideas on what I can try next?
Thanks
Brian

pons_saravanan
08-23-2009, 02:16 PM
Using C# codebehind in VS2008 I am trying to list and ultimately read/write files on my server (running mono), but located in a different folder to the root folder of my application

eg:
server_root/data_folder/data
vs
server_root/application_folder/application.aspx



Here is the code snippet I am using, which works when debugging on my local (windows) server but only shows files in the application folder (or ???? if I exclude Server.MapPath) when running on my remote server running mono


protected void Page_Load(object sender, EventArgs e)
{
string s = "";
string sFolder = "";
string[] MyDirList;
DirectoryInfo MyDir;
ArrayList CellarList = new ArrayList();

if (Request.IsLocal)
{
sFolder = "c:/";
MyDir = new DirectoryInfo(sFolder);
}
else
{
sFolder = "/";
MyDir = new DirectoryInfo(Server.MapPath(sFolder));
}
FileInfo[] MyFiles = MyDir.GetFiles("*.*");
foreach (FileInfo MyFile in MyFiles)
{
s = MyFile.Name;
CellarList.Add(new CellarFileData(s, s));
}

Repeater1.DataSource = CellarList;
Repeater1.DataBind();


(I am using a repeater to simply display the files till I work out the correct syntax)

Thanks for your help

you can impersonate a specific user to do this kind of works rather than giving full permissions,
please find the following code for understanding


using System.Security.Principal;
...
// Obtain the authenticated user's Identity
WindowsIdentity winId = (WindowsIdentity)getUserfromhereProbablyFromConfig //HttpContext.Current.User.Identity;
WindowsImpersonationContext ctx = null;
try
{
// Start impersonating
ctx = winId.Impersonate();
// Now impersonating
// Access resources using the identity of the authenticated user
}
// Prevent exceptions from propagating
catch
{
}
finally
{
// Revert impersonation
if (ctx != null)
ctx.Undo();
}

Uncork
08-24-2009, 01:54 AM
Thanks for the thought, but the folder I'm trying to access is on my web server (unix running mono) and has full public access. The users accessing it do not have a user id on this computer, so public access is appropriate.

The folder permissions do allow public access because I already access it via a perl script

Any other ideas?
Brian