System.IO?

A

Arpan

If I am not wrong, System.IO is one of the seven namespaces that get
imported in all ASP.NET page automatically but in the following code:

<%@ Import Namespace="System.IO" %>
<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim ioFileInfo As New FileInfo("C:\Inetpub\wwwroot\File1.asp")

lblOutput.Text = "Name: " & ioFileInfo.Name & "<br>"
lblOutput.Text += "Path: " & ioFileInfo.DirectoryName & "<br>"
lblOutput.Text += Last Accessed: " & ioFileInfo.LastAccessTime
& "<br>"
lblOutput.Text += Last Written: " & ioFileInfo.LastWriteTime &
"<br>"
lblOutput.Text += Size: " & ioFileInfo.Length & " bytes"
End Sub
</script>
<form runat="server">
<asp:Label ID="lblOutput" runat="server">
</form>

if I get rid of the <%@ Import.....%> line, then ASP.NET generates the
following error:

Type 'FileInfo' is not defined.

pointing to the very first line within the Page_Load sub (Dim
ioFileInfo As New FileInfo...).

Why?

Please correct me if I am wrong.

Thanks,

Arpan
 
J

Juan T. Llibre

re:
If I am not wrong, System.IO is one of the seven namespaces
that get imported in all ASP.NET page automatically

You are wrong.

If you wish to use System.IO, you must import it explicitly.
 
A

Arpan

Thanks Juan for pointing out my mistake. Actually the ASP.NET book I am
referring to learn ASP.NET states that System.IO is one of the seven
namespaces that get imported automatically by all ASP.NET pages - the
remaining 6 being

System
System.Collections
System.Web
System.Web.UI
System.Web.UI.HtmlControls
System.Web.UI.WebControls

I hope the above 6 namespaces "do" get imported automatically by all
ASP.NET pages. If not, then please let me know. God knows what all I
must have learnt wrong from that ASP.NET book!

BTW, are there any additional namespaces apart from the above 6 that
all ASP.NET pages import automatically?

Thanks once again,

Regards,

Arpan
 
G

Guest

Arpan,
If you do not see it in the using .. list (or "Import" in VB.NET) when you
create a class or web page, then you need to explicitly add it an ensure that
the project has a reference to the namespace assembly. If it isn't there, you
will find out about it very quickly!
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
 
J

Juan T. Llibre

In ASP.NET 2.0, you can control which namespaces are
imported automatically by configuring them in web.config.

Any namespace you define in web.config will be automatically imported,
and you will not have to import it specifically.

See :
http://msdn2.microsoft.com/en-us/library/system.web.configuration.namespacecollection.aspx
and
http://msdn2.microsoft.com/en-us/library/system.web.configuration.pagessection.aspx

re:
I hope the above 6 namespaces "do" get imported automatically by all ASP.NET pages.
If not, then please let me know.
BTW, are there any additional namespaces apart from the above 6 that
all ASP.NET pages import automatically?

In ASP.NET 2.0, the default namespaces are defined in the default web.config, located in :
Drive:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG

Here they are :

<pages>
<namespaces>
<add namespace="System" />
<add namespace="System.Collections" />
<add namespace="System.Collections.Specialized" />
<add namespace="System.Configuration" />
<add namespace="System.Text" />
<add namespace="System.Text.RegularExpressions" />
<add namespace="System.Web" />
<add namespace="System.Web.Caching" />
<add namespace="System.Web.SessionState" />
<add namespace="System.Web.Security" />
<add namespace="System.Web.Profile" />
<add namespace="System.Web.UI" />
<add namespace="System.Web.UI.WebControls" />
<add namespace="System.Web.UI.WebControls.WebParts" />
<add namespace="System.Web.UI.HtmlControls" />
</namespaces>
</pages>

You could add System.IO, so it's automatically imported, by adding :

<add namespace="System.IO" />

to the <namespaces> section of web.config.

Of course, any other namespace you want to import automatically can also be added.





Thanks Juan for pointing out my mistake. Actually the ASP.NET book I am
referring to learn ASP.NET states that System.IO is one of the seven
namespaces that get imported automatically by all ASP.NET pages - the
remaining 6 being

System
System.Collections
System.Web
System.Web.UI
System.Web.UI.HtmlControls
System.Web.UI.WebControls

I hope the above 6 namespaces "do" get imported automatically by all
ASP.NET pages. If not, then please let me know. God knows what all I
must have learnt wrong from that ASP.NET book!

BTW, are there any additional namespaces apart from the above 6 that
all ASP.NET pages import automatically?

Thanks once again,

Regards,

Arpan
 
K

Kevin Jones

Juan said:
re:

You are wrong.

If you wish to use System.IO, you must import it explicitly.
Look in <framework dir>\v2.0.50727\CONFIG\web.config.comments under the
compilation section to see the default imports (there are nine).

For 1.1 look in <framework dir>\v1.1.4322\CONFIG\machine.config. There
are eight listed there. The new one in 2.0 is System.Configuration.

Kevin Jones
 
J

Juan T. Llibre

re:
Look in <framework dir>\v2.0.50727\CONFIG\web.config.comments under the compilation section to see
the default imports (there are nine).

Kevin,

don't you consider mscorlib to be one of them ?

<add assembly="mscorlib" />

Also, doesn't the vbc compiler automatically import Microsoft.VisualBasic ?

Also, isn't the list supplied in the default web.config's pages element accurate ?

<pages>
<namespaces>
<add namespace="System" />
<add namespace="System.Collections" />
<add namespace="System.Collections.Specialized" />
<add namespace="System.Configuration" />
<add namespace="System.Text" />
<add namespace="System.Text.RegularExpressions" />
<add namespace="System.Web" />
<add namespace="System.Web.Caching" />
<add namespace="System.Web.SessionState" />
<add namespace="System.Web.Security" />
<add namespace="System.Web.Profile" />
<add namespace="System.Web.UI" />
<add namespace="System.Web.UI.WebControls" />
<add namespace="System.Web.UI.WebControls.WebParts" />
<add namespace="System.Web.UI.HtmlControls" />
</namespaces>
</pages>

Won't any namespace defined in web.config be automatically imported,
and you will not have to import it specifically ?

See :
http://msdn2.microsoft.com/en-us/library/system.web.configuration.namespacecollection.aspx
and
http://msdn2.microsoft.com/en-us/library/system.web.configuration.pagessection.aspx
 
K

Kevin Jones

don't you consider mscorlib to be one of them ?
>
> <add assembly="mscorlib" />

Oops, yep of course.
> Also, doesn't the vbc compiler automatically import
Microsoft.VisualBasic ?

Ah! I don't do VB, so I didn't look :)
> Also, isn't the list supplied in the default web.config's pages element accurate ?
>
> <pages>
> <namespaces>
> <add namespace="System" />
> <add namespace="System.Collections" />
> <add namespace="System.Collections.Specialized" />
> <add namespace="System.Configuration" />
> <add namespace="System.Text" />
> <add namespace="System.Text.RegularExpressions" />
> <add namespace="System.Web" />
> <add namespace="System.Web.Caching" />
> <add namespace="System.Web.SessionState" />
> <add namespace="System.Web.Security" />
> <add namespace="System.Web.Profile" />
> <add namespace="System.Web.UI" />
> <add namespace="System.Web.UI.WebControls" />
> <add namespace="System.Web.UI.WebControls.WebParts" />
> <add namespace="System.Web.UI.HtmlControls" />
> </namespaces>
> </pages>

Yep, but I was listing the assemblies not the namespaces, which of
course I should have been.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top