-1

I am trying to access a folder on server to get the files in it.

foreach (string filename in Directory.GetFiles(System.Web.HttpContext.Current.Server.MapPath(@"\\108.163.190.98:3306\home\mybizscard\Ads\")))
{
    list.Add(filename);
}

but I get this exception:

An unhandled exception of type 'System.NullReferenceException' occurred in WindowsFormsApplication1.exe

Additional information: Object reference not set to an instance of an object.

what is the problem ?? and how can I solve it ?

Community
  • 1
  • 1

3 Answers3

0

Are you sure you are giving right path I think it should be

foreach (string filename in Directory.GetFiles(System.Web.HttpContext.Current.Server.MapPath(@"\\108.163.190.98\home\mybizscard\Ads\")))
            {
                list.Add(filename);
            }
Dev D
  • 229
  • 1
  • 13
0

Seems you did not initialize your string array.

var files = new string[];
files = Directory.GetFiles(System.Web.HttpContext.Current.Server.MapPath(@"\\108.163.190.98:3306\home\mybizscard\Ads\"));
foreach (string filename in files)
            {
                list.Add(filename);
            }
fushigi
  • 81
  • 5
0

First of all, MapPath doesn't do what you think it does. It is used within ASP.NET applications to map a relative path to the web root (get an absolute path for the relative path).

From the documentation:

Returns the physical file path that corresponds to the specified virtual path on the Web server.

You can not use it within a desktop application. The question is why you want to do that anyway? A UNC path can already be used like any other path (it may not contain a port, however):

foreach (string filename in Directory.GetFiles(@"\\108.163.190.98\home\mybizscard\Ads\")))
{
    list.Add(filename);
}

That is: If the user running the application as the required rights on that folder.

Thorsten Dittmar
  • 52,871
  • 8
  • 78
  • 129