Showing posts with label file. Show all posts
Showing posts with label file. Show all posts

Saturday, March 31, 2012

System.Drawing: Error when using a .bmp or .gif file but not when .jpg

I each time get errors when I use an .bmp or ,.gif file as source but
whenever I use a .jpg file it works perfect.
Why is that? and how to fix it?
I almost have no knowledge about the system.drawing and so I am in the dark
about this.
This is my code
--
<%@dotnet.itags.org. Page Language="vb" %>
<%@dotnet.itags.org. import namespace="system.drawing" %>
<%@dotnet.itags.org. import namespace="system.drawing.imaging" %>
<%@dotnet.itags.org. import namespace="system.drawing.drawing2d" %>
<%
' initialise objects
dim strFilename as string
dim b as New System.Drawing.Bitmap(server.mappath("test.jpg"))
dim g as graphics = graphics.fromimage(b)
g.smoothingMode = smoothingMode.antiAlias
dim stringFormat As New StringFormat()
g.drawString("The Magic Word", New font("arial",3),systembrushes.windowtext,
New pointF(2,2), stringFormat)
' Set the content type
response.contenttype="image/jpeg"
' send the image to the viewer
b.save(response.outputstream, b.rawformat)
' tidy up
b.dispose()
%>Hi Richard,
As for the "A Graphics object cannot be created from an image that has an
indexed pixel format" error, it is caused by the Graphic object can't been
created from some certain image format which contains indexed pixel. I've
done some tests and one means is load the image and convert it into jpeg
format first and then create Graphich object to manipulate it. For example,
here is my test code:
========================================
==========
Private Sub Output_Image(ByVal path As String)
Try
Dim img As Image = Image.FromFile(path)
Dim stream As New System.IO.MemoryStream
img.Save(stream, ImageFormat.Jpeg)
Dim imgNew As Image = Image.FromStream(stream)
Dim g As Graphics = Graphics.FromImage(imgNew)
g.DrawString("MS Test", New Font("Verdana", 20,
FontStyle.Bold), _
New SolidBrush(Color.Beige), 0, 0)
Response.Clear()
Response.ContentType = "Image/jpeg"
imgNew.Save(Response.OutputStream, ImageFormat.Jpeg)
Response.End()
img.Dispose()
stream.Close()
imgNew.Dispose()
Catch ex As Exception
Response.Write("<br>" + ex.Message)
End Try
End Sub
========================================
=========
Please have a try to see whether it works for you. Thanks.
Regards,
Steven Cheng
Microsoft Online Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
thx a lot, this really helped me out.
I got no errors and with some thinking I made sure ppl can't link to my
files. (put a http://www. infront of url to view)
digifan.nl/ShowGalleryImage.aspx? img=Jlz1wB3JMO636rzX3rGMrpOK7mbHBTzFQQ1O
Q%2
fJ5V3Y%3d
and looking at the properties of the image it shows:
digifan.nl/ShowFullImage.aspx? img=Jlz1wB3JMO636rzX3rGMrpOK7mbHBTzFQQ1O
Q%2fJ5
V3Y%3d
which is only the image.
Again thx, i am going to read a bit some more about the system.drawing and
what it al can do but now i have an understanding of
the general thing.
Richard
"Steven Cheng[MSFT]" <v-schang@.online.microsoft.com> schreef in bericht
news:VQpV0n2UEHA.3204@.cpmsftngxa10.phx.gbl...
> Hi Richard,
> As for the "A Graphics object cannot be created from an image that has an
> indexed pixel format" error, it is caused by the Graphic object can't been
> created from some certain image format which contains indexed pixel. I've
> done some tests and one means is load the image and convert it into jpeg
> format first and then create Graphich object to manipulate it. For
example,
> here is my test code:
> ========================================
==========
> Private Sub Output_Image(ByVal path As String)
> Try
> Dim img As Image = Image.FromFile(path)
> Dim stream As New System.IO.MemoryStream
> img.Save(stream, ImageFormat.Jpeg)
> Dim imgNew As Image = Image.FromStream(stream)
> Dim g As Graphics = Graphics.FromImage(imgNew)
> g.DrawString("MS Test", New Font("Verdana", 20,
> FontStyle.Bold), _
> New SolidBrush(Color.Beige), 0, 0)
> Response.Clear()
> Response.ContentType = "Image/jpeg"
> imgNew.Save(Response.OutputStream, ImageFormat.Jpeg)
> Response.End()
>
> img.Dispose()
> stream.Close()
> imgNew.Dispose()
> Catch ex As Exception
> Response.Write("<br>" + ex.Message)
> End Try
> End Sub
> ========================================
=========
> Please have a try to see whether it works for you. Thanks.
> Regards,
> Steven Cheng
> Microsoft Online Support
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
> Get Preview at ASP.NET whidbey
> http://msdn.microsoft.com/asp.net/whidbey/default.aspx
>

System.Drawing: Error when using a .bmp or .gif file but not when .jpg

I each time get errors when I use an .bmp or ,.gif file as source but
whenever I use a .jpg file it works perfect.
Why is that? and how to fix it?

I almost have no knowledge about the system.drawing and so I am in the dark
about this.

This is my code
-----------

<%@dotnet.itags.org. Page Language="vb" %>
<%@dotnet.itags.org. import namespace="system.drawing" %>
<%@dotnet.itags.org. import namespace="system.drawing.imaging" %>
<%@dotnet.itags.org. import namespace="system.drawing.drawing2d" %>
<%

' initialise objects
dim strFilename as string
dim b as New System.Drawing.Bitmap(server.mappath("test.jpg"))
dim g as graphics = graphics.fromimage(b)
g.smoothingMode = smoothingMode.antiAlias
dim stringFormat As New StringFormat()
g.drawString("The Magic Word", New font("arial",3),systembrushes.windowtext,
New pointF(2,2), stringFormat)

' Set the content type
response.contenttype="image/jpeg"

' send the image to the viewer
b.save(response.outputstream, b.rawformat)

' tidy up
b.dispose()

%thx a lot, this really helped me out.

I got no errors and with some thinking I made sure ppl can't link to my
files. (put a http://www. infront of url to view)

digifan.nl/ShowGalleryImage.aspx?img=Jlz1wB3JMO636rzX3rGMrpOK 7mbHBTzFQQ1OQ%2
fJ5V3Y%3d

and looking at the properties of the image it shows:

digifan.nl/ShowFullImage.aspx?img=Jlz1wB3JMO636rzX3rGMrpOK7mb HBTzFQQ1OQ%2fJ5
V3Y%3d

which is only the image.

Again thx, i am going to read a bit some more about the system.drawing and
what it al can do but now i have an understanding of
the general thing.

Richard

"Steven Cheng[MSFT]" <v-schang@.online.microsoft.com> schreef in bericht
news:VQpV0n2UEHA.3204@.cpmsftngxa10.phx.gbl...
> Hi Richard,
> As for the "A Graphics object cannot be created from an image that has an
> indexed pixel format" error, it is caused by the Graphic object can't been
> created from some certain image format which contains indexed pixel. I've
> done some tests and one means is load the image and convert it into jpeg
> format first and then create Graphich object to manipulate it. For
example,
> here is my test code:
> ==================================================
> Private Sub Output_Image(ByVal path As String)
> Try
> Dim img As Image = Image.FromFile(path)
> Dim stream As New System.IO.MemoryStream
> img.Save(stream, ImageFormat.Jpeg)
> Dim imgNew As Image = Image.FromStream(stream)
> Dim g As Graphics = Graphics.FromImage(imgNew)
> g.DrawString("MS Test", New Font("Verdana", 20,
> FontStyle.Bold), _
> New SolidBrush(Color.Beige), 0, 0)
> Response.Clear()
> Response.ContentType = "Image/jpeg"
> imgNew.Save(Response.OutputStream, ImageFormat.Jpeg)
> Response.End()
>
> img.Dispose()
> stream.Close()
> imgNew.Dispose()
> Catch ex As Exception
> Response.Write("<br>" + ex.Message)
> End Try
> End Sub
> =================================================
> Please have a try to see whether it works for you. Thanks.
> Regards,
> Steven Cheng
> Microsoft Online Support
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
> Get Preview at ASP.NET whidbey
> http://msdn.microsoft.com/asp.net/whidbey/default.aspx
Hi Richard,

As for the "A Graphics object cannot be created from an image that has an
indexed pixel format" error, it is caused by the Graphic object can't been
created from some certain image format which contains indexed pixel. I've
done some tests and one means is load the image and convert it into jpeg
format first and then create Graphich object to manipulate it. For example,
here is my test code:
==================================================
Private Sub Output_Image(ByVal path As String)
Try

Dim img As Image = Image.FromFile(path)
Dim stream As New System.IO.MemoryStream
img.Save(stream, ImageFormat.Jpeg)
Dim imgNew As Image = Image.FromStream(stream)

Dim g As Graphics = Graphics.FromImage(imgNew)

g.DrawString("MS Test", New Font("Verdana", 20,
FontStyle.Bold), _
New SolidBrush(Color.Beige), 0, 0)

Response.Clear()
Response.ContentType = "Image/jpeg"

imgNew.Save(Response.OutputStream, ImageFormat.Jpeg)
Response.End()

img.Dispose()
stream.Close()
imgNew.Dispose()

Catch ex As Exception
Response.Write("<br>" + ex.Message)
End Try

End Sub
=================================================

Please have a try to see whether it works for you. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

System.FormatException: Invalid character in a Base-64 string...

Problem:
Enter the URL to a small image file (or any file), retrieve the file and encode the data as base64, then decode the base64 to get the original files data.

When the page below is executed, the file is retrieved and encoded, however when the decode_base64() subroutine is called, the system displays the following exception:

System.FormatException: Invalid character in a Base-64 string.
at System.Convert.FromBase64String(String s)
at ASP.test_base64_aspx.decode_base64()

Obviously, there is an invalid character in the Base-64 string -- the strange thing is if I increase "k_buf_size" from 4096 to something like 32000 (in the encode_base64 subroutine) and execute the page everything works just fine!!

So my question is, why will this code NOT work with smaller buffer sizes?? Hopefully, I have made a simple programming error that someone will point out as I have stared at this for too long ...

ANY help would be appreciated!

<%@dotnet.itags.org. Page Language="VB" Explicit="true" Strict="true" Trace="true" %>

<%@dotnet.itags.org. Import Namespace="System.IO" %>
<%@dotnet.itags.org. Import Namespace="System.Data" %>
<%@dotnet.itags.org. Import Namespace="System.Net" %>

<script language="VB" runat="server">

' The base64 encoded data ...
private m_encoded_data as StringBuilder

' The size of the file that was encoded ...
private m_file_size as Integer


'----------------
'
private sub do_test( Sender as Object, e as EventArgs )

' Init ...
m_encoded_data = new StringBuilder()
m_file_size = 0

' Attempt to encode the input file ...
encode_base64()

' If a file was encoded, attempt to decode ...
if ( m_encoded_data.length > 0 ) then

decode_base64()

end if

end sub


'----------------
'
private sub encode_base64()

const k_buf_size as Integer = 4096

dim bytes_read as Integer
dim n as Integer

dim err_msg as String
dim s as String
dim url as String

dim sr as Stream

dim wc as WebClient

Trace.Warn( ">", "==============" )
Trace.Warn( ">", "encode_base64:" )

' Get URL ...
url = fld_url.text.Trim()
if ( url.length = 0 )
exit sub
end if

' Attempt to get the data file and encode as base 64 ...
try

wc = Nothing
sr = Nothing
m_file_size = 0

' Create space to hold data ...
dim data_array( k_buf_size ) as Byte

' Create web client ...
wc = new WebClient()

' Create stream from URL ...
sr = wc.OpenRead( url )

' Loop to encode data stream ...

' Read a chunk from the data steam into array ...
bytes_read = sr.Read( data_array, 0, k_buf_size )

do while ( bytes_read > 0 )

Trace.Warn( ">", "bytes_read = " & bytes_read.ToString() )

m_file_size = m_file_size + bytes_read

' Convert data chunk to base 64 string. Remove any padding characters (=)
' that may have been added ...
s = System.Convert.ToBase64String( data_array, 0, bytes_read )
n = s.IndexOf( "=" )

if ( n > 0 ) then
m_encoded_data.Append( s.Substring( 0, n ) )
else
m_encoded_data.Append( s )
end if

bytes_read = sr.Read( data_array, 0, k_buf_size )

loop

' Encoded output length must be a multiple of 4 bytes, so pad if necessary ...
n = m_encoded_data.length Mod 4
if ( n <> 0 ) then
m_encoded_data.Append( new String( "="C, 4 - n ) )
end if

' Clean up ...
data_array = Nothing

Trace.Warn( ">", "File size = " & m_file_size.ToString() )
Trace.Warn( ">", "Encoded data length = " & m_encoded_data.length.ToString() )

catch ex as Exception
Trace.Warn( ">", ex.ToString() )

finally
if ( not sr is Nothing ) then
sr.Close()
sr = Nothing
end if
if ( not wc is Nothing ) then
wc.Dispose()
wc = Nothing
end if

end try

end sub


'----------------
'
private sub decode_base64()

dim data_array() as Byte

Trace.Warn( ">", "==============" )
Trace.Warn( ">", "decode_base64:" )

' Attempt to decode from base64 ...
try

data_array = Convert.FromBase64String( m_encoded_data.ToString() )

Trace.Warn( ">", "data_array.length = " & data_array.length.ToString() )

if ( data_array.length = m_file_size ) then
Trace.Warn( ">", "DECODE OK" )
else
Trace.Warn( ">", "DECODE FAILED!" )
end if

catch ex as Exception
Trace.Warn( ">", ex.ToString() )

end try

end sub
</script>
<html>
<head>
<title>Attempt to encode/decode using Base 64</title>
</head>
<body>
<blockquote>

<form runat="server">
Enter the URL to a file, preferably a small image JPG or GIF file.
<hr noshade size="1">
<table width="90%" border="0" cellpadding="2" cellspacing="1">
<tr>
<td width="20%" align="right" valign="top" >
URL:
</td>
<td width="80%" align="left" valign="top" >
<asp:TextBox id="fld_url" text="" size="100" maxlength="200" runat="server" />
</td>
</tr>
</table>
<hr noshade size="1">
<table width="90%" border="0" cellpadding="2" cellspacing="1">
<tr>
<td width="20%"> </td>
<td width="80%" valign="top">
<asp:Button OnClick="do_test" text="Do Base64 Test" runat="server" />
</td>
</tr>
</table>
</form>

</blockquote>
</body>
</html>

Ok - here is one solution (may not be the best, but it works)...

The problem with base 64 encoding is that one has to be careful when reading data in "chunks" and then encoding each chunk (as base 64) and then appending each chunk to form the final encoded string -- this is what I was doing initially. The code would work whenever I changed the buffer size to anything larger than the file being read, ie: whenever the entire file was read in one chunk. In any case, the solution is shown below for anyone that comes across this problem. Please note that the solution below limits the file sizes that can be encoded to 2 megabytes.

<%@. Page Language="VB" Explicit="true" Strict="true" Trace="true" %>

<%@. Import Namespace="System.IO" %>
<%@. Import Namespace="System.Data" %>
<%@. Import Namespace="System.Net" %>

<%@. Import Namespace="rentsys" %>

<script language="VB" runat="server">

' The base64 encoded data ...
private m_encoded_data as String

' The size of the file that was encoded ...
private m_file_size as Long


'----------------
'
private sub do_test( Sender as Object, e as EventArgs )

' Init ...
m_encoded_data = String.Empty
m_file_size = 0

' Attempt to encode the input file ...
encode_base64()

' If a file was encoded, attempt to decode ...
if ( m_encoded_data.length > 0 ) then

decode_base64()

end if

end sub


'----------------
'
private sub encode_base64()

' Maximum file size is 2 MB ...
const k_max_file_size as Long = 2000000

' Buffer used to read file in chunks ...
const k_data_buf_size as Integer = 4096
dim data_buf( k_data_buf_size ) as Byte

dim bytes_read as Integer

dim err_msg as String
dim s as String
dim url as String

dim ms as MemoryStream

dim sr as Stream

dim wc as WebClient


Trace.Warn( ">", "==============" )
Trace.Warn( ">", "encode_base64:" )

' Get URL ...
url = fld_url.text.Trim()
if ( url.length = 0 )
exit sub
end if


' Get the size of the remote file, unfortunately, the only way
' to reliably get this is to read the entire file. We could issue
' an HTTP HEAD request to get the content length, but some servers
' may not return file headers ...
try

wc = Nothing
sr = Nothing
m_file_size = 0

' Create web client ...
wc = new WebClient()

' Create stream from URL ...
sr = wc.OpenRead( url )

' Loop to determine file size ...
bytes_read = sr.Read( data_buf, 0, k_data_buf_size )
do while ( bytes_read > 0 )
m_file_size = m_file_size + bytes_read
bytes_read = sr.Read( data_buf, 0, k_data_buf_size )
loop

catch ex as Exception
Trace.Warn( ">", ex.ToString() )

finally
if ( not sr is Nothing ) then
sr.Close()
sr = Nothing
end if
if ( not wc is Nothing ) then
wc.Dispose()
wc = Nothing
end if

end try


' Check if we can handle the file size ...
Trace.Warn( ">", "File size = " & m_file_size.ToString() )

if ( m_file_size > k_max_file_size ) then
Trace.Warn( ">", "File size exceeds maximum allowed" )
exit sub
end if


' Attempt to read the file contents and encode as base 64 ...
try

wc = Nothing
sr = Nothing
ms = Nothing

' Create web client ...
wc = new WebClient()

' Create stream from URL ...
sr = wc.OpenRead( url )

' Create memory stream to hold file contents ...
ms = new MemoryStream()

' Loop to read data into memory stream. Note that we
' CANNOT convert each chunk read into base 64 and append
' to a string as this would lead to an invalid base 64
' string due to the nature of the algorithm and padding
' characters that would be present in the final string ...

bytes_read = sr.Read( data_buf, 0, k_data_buf_size )
do while ( bytes_read > 0 )

ms.Write( data_buf, 0, bytes_read )

bytes_read = sr.Read( data_buf, 0, k_data_buf_size )

loop

' Convert the data to base 64 ...
m_encoded_data = Convert.ToBase64String( ms.ToArray() )

catch ex as Exception
Trace.Warn( ">", ex.ToString() )

finally
if ( not ms is Nothing ) then
ms.Close()
ms = Nothing
end if
if ( not sr is Nothing ) then
sr.Close()
sr = Nothing
end if
if ( not wc is Nothing ) then
wc.Dispose()
wc = Nothing
end if

end try

end sub


'----------------
'
private sub decode_base64()

dim data_array() as Byte

Trace.Warn( ">", "==============" )
Trace.Warn( ">", "decode_base64:" )

' Attempt to decode from base64 ...
try

data_array = Convert.FromBase64String( m_encoded_data )

Trace.Warn( ">", "data_array.length = " & data_array.length.ToString() )

if ( data_array.length = m_file_size ) then
Trace.Warn( ">", "DECODE OK" )
else
Trace.Warn( ">", "DECODE FAILED!" )
end if

catch ex as Exception
Trace.Warn( ">", ex.ToString() )

end try

end sub
</script>
<html>
<head>
<title>Attempt to encode/decode using Base 64</title>
</head>
<body>
<blockquote>

<form runat="server">
Enter the URL to a file, preferably a small image JPG or GIF file.
<hr noshade size="1">
<table width="90%" border="0" cellpadding="2" cellspacing="1">
<tr>
<td width="20%" align="right" valign="top" class="form_label_1">
URL:
</td>
<td width="80%" align="left" valign="top" class="form_field_1">
<asp:TextBox id="fld_url" text="" size="100" maxlength="200" runat="server" />
</td>
</tr>
</table>
<hr noshade size="1">
<table width="90%" border="0" cellpadding="2" cellspacing="1">
<tr>
<td width="20%"> </td>
<td width="80%" valign="top">
<asp:Button OnClick="do_test" text="Do Base64 Test" runat="server" />
</td>
</tr>
</table>
</form>

</blockquote>
</body>
</html>

Wednesday, March 28, 2012

system.IO

code snippet:-

sFile = x:\test\abcd.mdb

if (System.IO.File.Exists(sFile))
//do something
else
//do something else

my code is failing at Exists function. It does not find the file.

X:\ is a mapped drive. X drive has domain account permissions (like
domain\myLoginaccount). also I am runing my web application with default
security as annonymus user called domain\myLoginaccount

Why the Exsits function does not find the file. Am I missing any permissions
here...whats the best mthod to access the files from another machine thru
asp.net

thanks

cheers !I think there are 2 potential hurdles here:

1. does the account running the web page have privileges to access the
share? Be aware that as well as the IIS user and IIS impersonation, you also
have to consider which user the asp.net worker process is running as. This
is quite a complex subject and I'm afraid my knowledge is limited to the
general concept.

2. does the web app actually have the drive mapped? the web server will not
inherit mapped drives from a terminal session so unless you are mapping the
drive from the web app, I don't tihnk it will be there. I would suggest you
try again with a unc path.

Andy

"abcd" <abcd@.abcd.com> wrote in message
news:eXNa%23BpoFHA.3912@.TK2MSFTNGP10.phx.gbl...
> code snippet:-
> sFile = x:\test\abcd.mdb
> if (System.IO.File.Exists(sFile))
> //do something
> else
> //do something else
> my code is failing at Exists function. It does not find the file.
> X:\ is a mapped drive. X drive has domain account permissions (like
> domain\myLoginaccount). also I am runing my web application with default
> security as annonymus user called domain\myLoginaccount
> Why the Exsits function does not find the file. Am I missing any
> permissions here...whats the best mthod to access the files from another
> machine thru asp.net
> thanks
> cheers !
>

system.IO

code snippet:-
sFile = x:\test\abcd.mdb
if (System.IO.File.Exists(sFile))
//do something
else
//do something else
my code is failing at Exists function. It does not find the file.
X:\ is a mapped drive. X drive has domain account permissions (like
domain\myLoginaccount). also I am runing my web application with default
security as annonymus user called domain\myLoginaccount
Why the Exsits function does not find the file. Am I missing any permissions
here...whats the best mthod to access the files from another machine thru
asp.net
thanks
cheers !I think there are 2 potential hurdles here:
1. does the account running the web page have privileges to access the
share? Be aware that as well as the IIS user and IIS impersonation, you also
have to consider which user the asp.net worker process is running as. This
is quite a complex subject and I'm afraid my knowledge is limited to the
general concept.
2. does the web app actually have the drive mapped? the web server will not
inherit mapped drives from a terminal session so unless you are mapping the
drive from the web app, I don't tihnk it will be there. I would suggest you
try again with a unc path.
Andy
"abcd" <abcd@.abcd.com> wrote in message
news:eXNa%23BpoFHA.3912@.TK2MSFTNGP10.phx.gbl...
> code snippet:-
> sFile = x:\test\abcd.mdb
> if (System.IO.File.Exists(sFile))
> //do something
> else
> //do something else
> my code is failing at Exists function. It does not find the file.
> X:\ is a mapped drive. X drive has domain account permissions (like
> domain\myLoginaccount). also I am runing my web application with default
> security as annonymus user called domain\myLoginaccount
> Why the Exsits function does not find the file. Am I missing any
> permissions here...whats the best mthod to access the files from another
> machine thru asp.net
> thanks
> cheers !
>
>

system.Io function

hi, i want to know whether a file exists are not in a
particular directory.

how to write a function for it in asp.net using vbFile.Exists(path)

HTH

Thanks,

sswalia
MCAD, MCSD, OCA

"rahim" <rahim786@.rediffmail.com> wrote in message
news:059d01c3c523$20c4c700$a601280a@.phx.gbl...
> hi, i want to know whether a file exists are not in a
> particular directory.
> how to write a function for it in asp.net using vb
If System.IO.File.Exists("C:\somefile.txt") then
'it exists
End If

--
Jerry Boone
Analytical Technologies, Inc.
http://www.antech.biz

"rahim" <rahim786@.rediffmail.com> wrote in message
news:059d01c3c523$20c4c700$a601280a@.phx.gbl...
> hi, i want to know whether a file exists are not in a
> particular directory.
> how to write a function for it in asp.net using vb

System.io.File

When using the File object how do you now whether the path you are putting, is the path of a file on the server or the path of a file on the client machine.

I want to transfer a file from the client machine to the server, does anyone know if the File object is suited for this?I guess the real question there is where did you get the path from.

If the path came from the Posted File - then it is most likely the file path / name from the client not and is not valid for saving on the server
What do you mean when you say where the path came from, what if i hard code a file path to be e.g c:/intepub/newfolder/file.txt

If i put that path into a file object, would that look at c: on the server or client?
It's always a Path on your server or another machine in your network, over which you (or rather the ASPNET worker process or some other user you let the worker process impersonate) have rights to access.

The only time you handle a client path, is when someone has uploaded a file to your server.
Ok, but thats what i want to do using asp.net, i want to allow users to upload a file from their machine to the server. I have done this successfully using Java, but wanted to know if its possible using .net
It's extremely easy using .Net. One line of code will suffice in this very simple scenario:


<%@. Page Language="VB" %>
<%@. import Namespace="System.IO" %>
<script runat="server">
Sub Button_Click( obj As Object, evnt As EventArgs )
inpFileName.PostedFile.SaveAs( Server.MapPath("") & Path.GetFileName(inpFileName.PostedFile.FileName) )
End Sub
</script>
<html>
<head>
<title>Upload file</title>
</head>
<body>
<form enctype="multipart/form-data" runat="Server">
<input id="inpFileName" type="file" runat="Server" /><br>
<asp:Button id="btnFileUp" onclick="Button_Click" Runat="Server" Text="Upload file" /></form>
</body>
</html>

This will save an uploaded file to the root of your web application. Remember to grant rights to write to the physical folder for the ASPNET account. By default the max file size to be uploaded using .Net is set to 4 mb (4096 kb). If you need to up this size, you set it in your application's Web.config file using:

<httpRuntime maxRequestLength="<size in kb you wish to allow>" />

Thanks very much

System.IO.File class and IP paths?

I try to use System.IO.File.Copy where the source is
"\\10.100.100.1\myfolder\test.txt"

When running the aspx file on the server (the server is
also the client) it works, but when using another NT
client it does not work.
The err.description says "Could not find file ..."
System.IO.File.Exists return false here.

But If I manually paste in \\10.100.100.1
\myfolder\test.txt in the IE address field and press "Go"
the file is found.

Strange or not?Hi,

I don't think that the default asp.net user got rights to access network
resources.

Natty Gur[MVP]
Phone Numbers:
Office: +972-(0)9-7740261
Fax: +972-(0)9-7740261
Mobile: +972-(0)58-888377

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

system.IO.File.Exists doesnt working for file that is outside my virtual directory

hi

i have file browser control to select any file and a button to upload
file on my web page now when i select any file. now on click of upload
button i have check that file exist or no

if system.IO.File.Exist(file path) then
...
end if
now when ever i choose file system.IO.File.Exist(file path) return
false, but if i select file from my virtual directory then only it
return true.

so how to select file from any folder on my PC and chech that it exist
or notIt may have to do with the ASP.net user in IIS not having read
permissions on the directories?

Max wrote:

Quote:

Originally Posted by

hi
>
i have file browser control to select any file and a button to upload
file on my web page now when i select any file. now on click of upload
button i have check that file exist or no
>
if system.IO.File.Exist(file path) then
...
end if
now when ever i choose file system.IO.File.Exist(file path) return
false, but if i select file from my virtual directory then only it
return true.
>
so how to select file from any folder on my PC and chech that it exist
or not


Are you using an absolute path or a relative path to the file?

"Max" wrote:

Quote:

Originally Posted by

hi
>
i have file browser control to select any file and a button to upload
file on my web page now when i select any file. now on click of upload
button i have check that file exist or no
>
if system.IO.File.Exist(file path) then
...
end if
now when ever i choose file system.IO.File.Exist(file path) return
false, but if i select file from my virtual directory then only it
return true.
>
so how to select file from any folder on my PC and chech that it exist
or not
>
>


followin is the error i am getting

Server Error in '/' Application.
------------------------

Access to the path
"L:\Webhosting\home.gujarat.gov.in\homeapps\imagefi les\images.jpg" is
denied.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.UnauthorizedAccessException: Access to the
path "L:\Webhosting\home.gujarat.gov.in\homeapps\imagefi les\images.jpg"
is denied.

ASP.NET is not authorized to access the requested resource. Consider
granting access rights to the resource to the ASP.NET request identity.
ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS
5 or Network Service on IIS 6) that is used if the application is not
impersonating. If the application is impersonating via <identity
impersonate="true"/>, the identity will be the anonymous user
(typically IUSR_MACHINENAME) or the authenticated request user.

To grant ASP.NET write access to a file, right-click the file in
Explorer, choose "Properties" and select the Security tab. Click "Add"
to add the appropriate user or group. Highlight the ASP.NET account,
and check the boxes for the desired access.

Source Error:

An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of
the exception can be identified using the exception stack trace below.

Stack Trace:

[UnauthorizedAccessException: Access to the path
"L:\Webhosting\home.gujarat.gov.in\homeapps\imagefi les\images.jpg" is
denied.]
System.IO.__Error.WinIOError(Int32 errorCode, String str) +393
System.IO.File.Delete(String path) +165
CMSAPPS.mas_WMKD_entry.Save_ADD() in
D:\dnSource\HOMEAPPS_mahesh\mas_WMKD_entry.aspx.vb :248
CMSAPPS.mas_WMKD_entry.bttn_Save_Click(Object sender, EventArgs e)
in D:\dnSource\HOMEAPPS_mahesh\mas_WMKD_entry.aspx.vb :221
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108

System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String
eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData)
+33
System.Web.UI.Page.ProcessRequestMain() +1292
What do you not understand about the exception's explanation?

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Orange you bland I stopped splaying bananas?

"Max" <mahesh.anjani@.gmail.comwrote in message
news:1155306880.304856.71550@.i3g2000cwc.googlegrou ps.com...

Quote:

Originally Posted by

followin is the error i am getting
>
Server Error in '/' Application.
------------------------
>
Access to the path
"L:\Webhosting\home.gujarat.gov.in\homeapps\imagefi les\images.jpg" is
denied.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
>
Exception Details: System.UnauthorizedAccessException: Access to the
path "L:\Webhosting\home.gujarat.gov.in\homeapps\imagefi les\images.jpg"
is denied.
>
ASP.NET is not authorized to access the requested resource. Consider
granting access rights to the resource to the ASP.NET request identity.
ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS
5 or Network Service on IIS 6) that is used if the application is not
impersonating. If the application is impersonating via <identity
impersonate="true"/>, the identity will be the anonymous user
(typically IUSR_MACHINENAME) or the authenticated request user.
>
To grant ASP.NET write access to a file, right-click the file in
Explorer, choose "Properties" and select the Security tab. Click "Add"
to add the appropriate user or group. Highlight the ASP.NET account,
and check the boxes for the desired access.
>
Source Error:
>
An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of
the exception can be identified using the exception stack trace below.
>
>
Stack Trace:
>
>
[UnauthorizedAccessException: Access to the path
"L:\Webhosting\home.gujarat.gov.in\homeapps\imagefi les\images.jpg" is
denied.]
System.IO.__Error.WinIOError(Int32 errorCode, String str) +393
System.IO.File.Delete(String path) +165
CMSAPPS.mas_WMKD_entry.Save_ADD() in
D:\dnSource\HOMEAPPS_mahesh\mas_WMKD_entry.aspx.vb :248
CMSAPPS.mas_WMKD_entry.bttn_Save_Click(Object sender, EventArgs e)
in D:\dnSource\HOMEAPPS_mahesh\mas_WMKD_entry.aspx.vb :221
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
>
System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String
eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData)
+33
System.Web.UI.Page.ProcessRequestMain() +1292
>

System.IO.FileNotFoundException, When Accessing file in remote ser

I am trying to access files in a remote server from my ASP.NET application,
it is giving System.IO.FileNotFoundException error. I tried mapping that
server location like "H:\ErrorLogs\error.log" still it gives error. I tried
even giving the server name and directory like
"\\server_name\\Directory\\file.log" still it gives file not found exception.
Does anyone help me fix this.

Thanksuse \\machineIP\sharename\filename

"Raja" <Raja@.discussions.microsoft.com> wrote in message
news:DC182C99-4D78-4A68-BEDC-632DDE49A827@.microsoft.com...
> I am trying to access files in a remote server from my ASP.NET
application,
> it is giving System.IO.FileNotFoundException error. I tried mapping that
> server location like "H:\ErrorLogs\error.log" still it gives error. I
tried
> even giving the server name and directory like
> "\\server_name\\Directory\\file.log" still it gives file not found
exception.
> Does anyone help me fix this.
> Thanks
asp.net normally does not have permission to any network resources. to
access a networked drive, you will have to make the asp_net account a domain
account, or specify the userName and password of a domain account in the web
config <impersonate> tag.

-- bruce (sqlwork.com)

"Raja" <Raja@.discussions.microsoft.com> wrote in message
news:DC182C99-4D78-4A68-BEDC-632DDE49A827@.microsoft.com...
> I am trying to access files in a remote server from my ASP.NET
application,
> it is giving System.IO.FileNotFoundException error. I tried mapping that
> server location like "H:\ErrorLogs\error.log" still it gives error. I
tried
> even giving the server name and directory like
> "\\server_name\\Directory\\file.log" still it gives file not found
exception.
> Does anyone help me fix this.
> Thanks

System.IO.FileNotFoundException, When Accessing file in remote ser

I am trying to access files in a remote server from my ASP.NET application,
it is giving System.IO.FileNotFoundException error. I tried mapping that
server location like "H:\ErrorLogs\error.log" still it gives error. I tried
even giving the server name and directory like
"\\server_name\\Directory\\file.log" still it gives file not found exception
.
Does anyone help me fix this.
Thanksuse \\machineIP\sharename\filename
"Raja" <Raja@.discussions.microsoft.com> wrote in message
news:DC182C99-4D78-4A68-BEDC-632DDE49A827@.microsoft.com...
> I am trying to access files in a remote server from my ASP.NET
application,
> it is giving System.IO.FileNotFoundException error. I tried mapping that
> server location like "H:\ErrorLogs\error.log" still it gives error. I
tried
> even giving the server name and directory like
> "\\server_name\\Directory\\file.log" still it gives file not found
exception.
> Does anyone help me fix this.
> Thanks
asp.net normally does not have permission to any network resources. to
access a networked drive, you will have to make the asp_net account a domain
account, or specify the userName and password of a domain account in the web
config <impersonate> tag.
-- bruce (sqlwork.com)
"Raja" <Raja@.discussions.microsoft.com> wrote in message
news:DC182C99-4D78-4A68-BEDC-632DDE49A827@.microsoft.com...
> I am trying to access files in a remote server from my ASP.NET
application,
> it is giving System.IO.FileNotFoundException error. I tried mapping that
> server location like "H:\ErrorLogs\error.log" still it gives error. I
tried
> even giving the server name and directory like
> "\\server_name\\Directory\\file.log" still it gives file not found
exception.
> Does anyone help me fix this.
> Thanks

System.IO.IOException: Cannot create a file when that file already exists.

Getting the following error on my production server whether the file exists
or not:

"System.IO.IOException: Cannot create a file when that file already exists."

Here's the code generating the error (seems to be happening when I try
creating a directory)

If dirmgr.Exists("s:\blah\" & txt_name.Text) Then
lblerror.Text = lblerror.Text & "Unable to build physical path. " &
txt_name.Text & " & Contact Dev Team.<BR>"
Else
dirmgr.CreateDirectory("s:\blah\" & txt_name.Text)

This code runs fine on my development server (only the path is c:\blah\blah
instead of S). Also, my production server has failover cluser, and the s
drive is the failover drive. I can't imagine that could be a problem, but
thought I would mention it.

Asside from that, the only other thing I can think of is that the framework
version on my dev box says 1.1, and the one on my production server says:
1.0.37 - surely that's not the cause is it? Permissions shouldn't be an
issue (i've even tried as domain admin)

I would appreciate any help on this, as I'm out of ideas, and can find no
other documentation on this.
- ChadHi,

the default user that runs web application (system) dont has rights to
access network resources.

Natty Gur, CTO
Dao2Com Ltd.
34th Elkalay st. Raanana
Israel , 43000
Phone Numbers:
Office: +972-(0)9-7740261
Fax: +972-(0)9-7740261
Mobile: +972-(0)58-888377

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
I agree with Natty. The problem you are having is most likely caused by
having no right to access network resources. Willy address a similar
problem before. See
http://groups.google.com/groups?q=S...et&start=10&hl=
en&lr=&ie=UTF-8&oe=UTF-8&selm=OELVHLBNCHA.2460%40tkmsftngp04&rnum=20.

HTH,
-Allen

Disclaimer:
This posting is provided "AS IS" with no warranties, and confers no rights.
Got .Net? http://www.gotdotnet.com

-------
| From: "Chad Crowder" <chad.crowder@.gis.leica-geosystems.com>
| Subject: System.IO.IOException: Cannot create a file when that file
already exists.
| Date: Wed, 6 Aug 2003 17:08:27 -0400
| Lines: 29
|
| Getting the following error on my production server whether the file
exists
| or not:
|
| "System.IO.IOException: Cannot create a file when that file already
exists."
|
| Here's the code generating the error (seems to be happening when I try
| creating a directory)
|
| If dirmgr.Exists("s:\blah\" & txt_name.Text) Then
| lblerror.Text = lblerror.Text & "Unable to build physical path. "
&
| txt_name.Text & " & Contact Dev Team.<BR>"
| Else
| dirmgr.CreateDirectory("s:\blah\" & txt_name.Text)
|
| This code runs fine on my development server (only the path is
c:\blah\blah
| instead of S). Also, my production server has failover cluser, and the s
| drive is the failover drive. I can't imagine that could be a problem, but
| thought I would mention it.
|
| Asside from that, the only other thing I can think of is that the
framework
| version on my dev box says 1.1, and the one on my production server says:
| 1.0.37 - surely that's not the cause is it? Permissions shouldn't be an
| issue (i've even tried as domain admin)
|
| I would appreciate any help on this, as I'm out of ideas, and can find no
| other documentation on this.
| - Chad
|
|
|
Thanks for the response.

I understand that, but I'm trying to create a directory on the local drive.
This isn't a mapped share. The drive is physically present on the web
server, and if that web server fails, the other system physically takes over
the drive (ie, a cluster). I've also added the local ASPNET account to the
NTFS permissions on the folder, and it still fails.

- Chad

"[MSFT]Allen" <yweng@.online.microsoft.com> wrote in message
news:j1MRYHMXDHA.2108@.cpmsftngxa06.phx.gbl...
> I agree with Natty. The problem you are having is most likely caused by
> having no right to access network resources. Willy address a similar
> problem before. See
http://groups.google.com/groups?q=S...et&start=10&hl=
> en&lr=&ie=UTF-8&oe=UTF-8&selm=OELVHLBNCHA.2460%40tkmsftngp04&rnum=20.
> HTH,
> -Allen
> Disclaimer:
> This posting is provided "AS IS" with no warranties, and confers no
rights.
> Got .Net? http://www.gotdotnet.com
> -------
> | From: "Chad Crowder" <chad.crowder@.gis.leica-geosystems.com>
> | Subject: System.IO.IOException: Cannot create a file when that file
> already exists.
> | Date: Wed, 6 Aug 2003 17:08:27 -0400
> | Lines: 29
> |
> | Getting the following error on my production server whether the file
> exists
> | or not:
> |
> | "System.IO.IOException: Cannot create a file when that file already
> exists."
> |
> | Here's the code generating the error (seems to be happening when I try
> | creating a directory)
> |
> | If dirmgr.Exists("s:\blah\" & txt_name.Text) Then
> | lblerror.Text = lblerror.Text & "Unable to build physical path.
"
> &
> | txt_name.Text & " & Contact Dev Team.<BR>"
> | Else
> | dirmgr.CreateDirectory("s:\blah\" & txt_name.Text)
> |
> | This code runs fine on my development server (only the path is
> c:\blah\blah
> | instead of S). Also, my production server has failover cluser, and the
s
> | drive is the failover drive. I can't imagine that could be a problem,
but
> | thought I would mention it.
> |
> | Asside from that, the only other thing I can think of is that the
> framework
> | version on my dev box says 1.1, and the one on my production server
says:
> | 1.0.37 - surely that's not the cause is it? Permissions shouldn't be an
> | issue (i've even tried as domain admin)
> |
> | I would appreciate any help on this, as I'm out of ideas, and can find
no
> | other documentation on this.
> | - Chad
> |
> |
> |
It doesn't work creating folders under the same directory as the web
application... same issue.

I'm going to try figuring out why that server is showing that it's running
version 1.03 of the dotnet framework. It shows that v1.1 was installed.

Thanks,
Chad

"[MSFT]Allen" <yweng@.online.microsoft.com> wrote in message
news:RT0i6ZXXDHA.2184@.cpmsftngxa06.phx.gbl...
> Try creating a directory in your code under the folder where your web
> application resides. Does it works? In addition, you may want to verify
the
> account your web application is running under. Please download tokedump
> written by Keith Brown from
> http://msdn.microsoft.com/msdnmag/i...ty/default.aspx. It
> would show you what account the application is running under.
> -Allen
> Disclaimer:
> This posting is provided "AS IS" with no warranties, and confers no
rights.
> Got .Net? http://www.gotdotnet.com
> -------
> | From: "Chad Crowder" <chad.crowder@.gis.leica-geosystems.com>
> | References: <uaJhi7FXDHA.1900@.TK2MSFTNGP10.phx.gbl>
> <j1MRYHMXDHA.2108@.cpmsftngxa06.phx.gbl>
> | Subject: Re: System.IO.IOException: Cannot create a file when that file
> already exists.
> | Date: Thu, 7 Aug 2003 08:11:44 -0400
> | Lines: 78
> | X-Priority: 3
> | X-MSMail-Priority: Normal
> | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
> | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
> | Message-ID: <exrIT0NXDHA.1204@.TK2MSFTNGP12.phx.gbl>
> | Newsgroups: microsoft.public.dotnet.framework.aspnet
> | NNTP-Posting-Host: 12.108.244.117
> | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
> | Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.aspnet:165699
> | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
> |
> | Thanks for the response.
> |
> | I understand that, but I'm trying to create a directory on the local
> drive.
> | This isn't a mapped share. The drive is physically present on the web
> | server, and if that web server fails, the other system physically takes
> over
> | the drive (ie, a cluster). I've also added the local ASPNET account to
> the
> | NTFS permissions on the folder, and it still fails.
> |
> | - Chad
> |
> |
> | "[MSFT]Allen" <yweng@.online.microsoft.com> wrote in message
> | news:j1MRYHMXDHA.2108@.cpmsftngxa06.phx.gbl...
> | > I agree with Natty. The problem you are having is most likely caused
by
> | > having no right to access network resources. Willy address a similar
> | > problem before. See
> | >
> |
http://groups.google.com/groups?q=S...et&start=10&hl=
> | > en&lr=&ie=UTF-8&oe=UTF-8&selm=OELVHLBNCHA.2460%40tkmsftngp04&rnum=20.
> | >
> | > HTH,
> | > -Allen
> | >
> | > Disclaimer:
> | > This posting is provided "AS IS" with no warranties, and confers no
> | rights.
> | > Got .Net? http://www.gotdotnet.com
> | >
> | > -------
> | > | From: "Chad Crowder" <chad.crowder@.gis.leica-geosystems.com>
> | > | Subject: System.IO.IOException: Cannot create a file when that file
> | > already exists.
> | > | Date: Wed, 6 Aug 2003 17:08:27 -0400
> | > | Lines: 29
> | > |
> | > | Getting the following error on my production server whether the file
> | > exists
> | > | or not:
> | > |
> | > | "System.IO.IOException: Cannot create a file when that file already
> | > exists."
> | > |
> | > | Here's the code generating the error (seems to be happening when I
try
> | > | creating a directory)
> | > |
> | > | If dirmgr.Exists("s:\blah\" & txt_name.Text) Then
> | > | lblerror.Text = lblerror.Text & "Unable to build physical
> path.
> | "
> | > &
> | > | txt_name.Text & " & Contact Dev Team.<BR>"
> | > | Else
> | > | dirmgr.CreateDirectory("s:\blah\" & txt_name.Text)
> | > |
> | > | This code runs fine on my development server (only the path is
> | > c:\blah\blah
> | > | instead of S). Also, my production server has failover cluser, and
> the
> | s
> | > | drive is the failover drive. I can't imagine that could be a
problem,
> | but
> | > | thought I would mention it.
> | > |
> | > | Asside from that, the only other thing I can think of is that the
> | > framework
> | > | version on my dev box says 1.1, and the one on my production server
> | says:
> | > | 1.0.37 - surely that's not the cause is it? Permissions shouldn't
be
> an
> | > | issue (i've even tried as domain admin)
> | > |
> | > | I would appreciate any help on this, as I'm out of ideas, and can
find
> | no
> | > | other documentation on this.
> | > | - Chad
> | > |
> | > |
> | > |
> | >
> |
> |
> |
Chad, was the problem resolved? It seems that you installed both v1.03 and
v1.1 on the same web server. If so, it might be helpful to look at the
article "Compatibility Considerations and Version Changes" on
http://www.gotdotnet.com/team/changeinfo/default.aspx. Try configuring your
web application to run V1.03 and V1.1 specifically to see how it works.

-Allen

Disclaimer:
This posting is provided "AS IS" with no warranties, and confers no rights.
Got .Net? http://www.gotdotnet.com

-------
| From: "Chad Crowder" <chad.crowder@.gis.leica-geosystems.com>
| References: <uaJhi7FXDHA.1900@.TK2MSFTNGP10.phx.gbl>
<j1MRYHMXDHA.2108@.cpmsftngxa06.phx.gbl>
<exrIT0NXDHA.1204@.TK2MSFTNGP12.phx.gbl>
<RT0i6ZXXDHA.2184@.cpmsftngxa06.phx.gbl>
| Subject: Re: System.IO.IOException: Cannot create a file when that file
already exists.
| Date: Mon, 11 Aug 2003 01:16:22 -0400
| Lines: 141
|
|
| It doesn't work creating folders under the same directory as the web
| application... same issue.
|
| I'm going to try figuring out why that server is showing that it's running
| version 1.03 of the dotnet framework. It shows that v1.1 was installed.
|
| Thanks,
| Chad
|
| "[MSFT]Allen" <yweng@.online.microsoft.com> wrote in message
| news:RT0i6ZXXDHA.2184@.cpmsftngxa06.phx.gbl...
| > Try creating a directory in your code under the folder where your web
| > application resides. Does it works? In addition, you may want to verify
| the
| > account your web application is running under. Please download tokedump
| > written by Keith Brown from
| > http://msdn.microsoft.com/msdnmag/i...ty/default.aspx. It
| > would show you what account the application is running under.
| >
| > -Allen
| >
| > Disclaimer:
| > This posting is provided "AS IS" with no warranties, and confers no
| rights.
| > Got .Net? http://www.gotdotnet.com
| >
| > -------
| > | From: "Chad Crowder" <chad.crowder@.gis.leica-geosystems.com>
| > | References: <uaJhi7FXDHA.1900@.TK2MSFTNGP10.phx.gbl>
| > <j1MRYHMXDHA.2108@.cpmsftngxa06.phx.gbl>
| > | Subject: Re: System.IO.IOException: Cannot create a file when that
file
| > already exists.
| > | Date: Thu, 7 Aug 2003 08:11:44 -0400
| > | Lines: 78
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <exrIT0NXDHA.1204@.TK2MSFTNGP12.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | NNTP-Posting-Host: 12.108.244.117
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.framework.aspnet:165699
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > |
| > | Thanks for the response.
| > |
| > | I understand that, but I'm trying to create a directory on the local
| > drive.
| > | This isn't a mapped share. The drive is physically present on the
web
| > | server, and if that web server fails, the other system physically
takes
| > over
| > | the drive (ie, a cluster). I've also added the local ASPNET account
to
| > the
| > | NTFS permissions on the folder, and it still fails.
| > |
| > | - Chad
| > |
| > |
| > | "[MSFT]Allen" <yweng@.online.microsoft.com> wrote in message
| > | news:j1MRYHMXDHA.2108@.cpmsftngxa06.phx.gbl...
| > | > I agree with Natty. The problem you are having is most likely caused
| by
| > | > having no right to access network resources. Willy address a similar
| > | > problem before. See
| > | >
| > |
| >
|
http://groups.google.com/groups?q=S...et&start=10&hl=
| > | >
en&lr=&ie=UTF-8&oe=UTF-8&selm=OELVHLBNCHA.2460%40tkmsftngp04&rnum=20.
| > | >
| > | > HTH,
| > | > -Allen
| > | >
| > | > Disclaimer:
| > | > This posting is provided "AS IS" with no warranties, and confers no
| > | rights.
| > | > Got .Net? http://www.gotdotnet.com
| > | >
| > | > -------
| > | > | From: "Chad Crowder" <chad.crowder@.gis.leica-geosystems.com>
| > | > | Subject: System.IO.IOException: Cannot create a file when that
file
| > | > already exists.
| > | > | Date: Wed, 6 Aug 2003 17:08:27 -0400
| > | > | Lines: 29
| > | > |
| > | > | Getting the following error on my production server whether the
file
| > | > exists
| > | > | or not:
| > | > |
| > | > | "System.IO.IOException: Cannot create a file when that file
already
| > | > exists."
| > | > |
| > | > | Here's the code generating the error (seems to be happening when I
| try
| > | > | creating a directory)
| > | > |
| > | > | If dirmgr.Exists("s:\blah\" & txt_name.Text) Then
| > | > | lblerror.Text = lblerror.Text & "Unable to build physical
| > path.
| > | "
| > | > &
| > | > | txt_name.Text & " & Contact Dev Team.<BR>"
| > | > | Else
| > | > | dirmgr.CreateDirectory("s:\blah\" & txt_name.Text)
| > | > |
| > | > | This code runs fine on my development server (only the path is
| > | > c:\blah\blah
| > | > | instead of S). Also, my production server has failover cluser,
and
| > the
| > | s
| > | > | drive is the failover drive. I can't imagine that could be a
| problem,
| > | but
| > | > | thought I would mention it.
| > | > |
| > | > | Asside from that, the only other thing I can think of is that the
| > | > framework
| > | > | version on my dev box says 1.1, and the one on my production
server
| > | says:
| > | > | 1.0.37 - surely that's not the cause is it? Permissions shouldn't
| be
| > an
| > | > | issue (i've even tried as domain admin)
| > | > |
| > | > | I would appreciate any help on this, as I'm out of ideas, and can
| find
| > | no
| > | > | other documentation on this.
| > | > | - Chad
| > | > |
| > | > |
| > | > |
| > | >
| > |
| > |
| > |
| >
|
|
|
Allen,

Thanks for the followup. I actually got my program working late last night.
The problem was the framework version.

I tried simply configuring the web application to use version 1.1, but that
didn't help. I also tried uninstalling then reinstalling framework version
1.1, but that also didn't get IIS to start using version 1.1 by default. I
consequently uninstalled versions 1.1 *and* version 1.03, then only
reinstalled version 1.1 again, and this took care of the problem.

Thanks again for all the help everyone!

- Chad

"[MSFT]Allen" <yweng@.online.microsoft.com> wrote in message
news:hNsBh8sYDHA.2352@.cpmsftngxa06.phx.gbl...
> Chad, was the problem resolved? It seems that you installed both v1.03 and
> v1.1 on the same web server. If so, it might be helpful to look at the
> article "Compatibility Considerations and Version Changes" on
> http://www.gotdotnet.com/team/changeinfo/default.aspx. Try configuring
your
> web application to run V1.03 and V1.1 specifically to see how it works.
> -Allen
> Disclaimer:
> This posting is provided "AS IS" with no warranties, and confers no
rights.
> Got .Net? http://www.gotdotnet.com
> -------
> | From: "Chad Crowder" <chad.crowder@.gis.leica-geosystems.com>
> | References: <uaJhi7FXDHA.1900@.TK2MSFTNGP10.phx.gbl>
> <j1MRYHMXDHA.2108@.cpmsftngxa06.phx.gbl>
> <exrIT0NXDHA.1204@.TK2MSFTNGP12.phx.gbl>
> <RT0i6ZXXDHA.2184@.cpmsftngxa06.phx.gbl>
> | Subject: Re: System.IO.IOException: Cannot create a file when that file
> already exists.
> | Date: Mon, 11 Aug 2003 01:16:22 -0400
> | Lines: 141
> |
> |
> | It doesn't work creating folders under the same directory as the web
> | application... same issue.
> |
> | I'm going to try figuring out why that server is showing that it's
running
> | version 1.03 of the dotnet framework. It shows that v1.1 was installed.
> |
> | Thanks,
> | Chad
> |
> | "[MSFT]Allen" <yweng@.online.microsoft.com> wrote in message
> | news:RT0i6ZXXDHA.2184@.cpmsftngxa06.phx.gbl...
> | > Try creating a directory in your code under the folder where your web
> | > application resides. Does it works? In addition, you may want to
verify
> | the
> | > account your web application is running under. Please download
tokedump
> | > written by Keith Brown from
> | > http://msdn.microsoft.com/msdnmag/i...ty/default.aspx.
It
> | > would show you what account the application is running under.
> | >
> | > -Allen
> | >
> | > Disclaimer:
> | > This posting is provided "AS IS" with no warranties, and confers no
> | rights.
> | > Got .Net? http://www.gotdotnet.com
> | >
> | > -------
> | > | From: "Chad Crowder" <chad.crowder@.gis.leica-geosystems.com>
> | > | References: <uaJhi7FXDHA.1900@.TK2MSFTNGP10.phx.gbl>
> | > <j1MRYHMXDHA.2108@.cpmsftngxa06.phx.gbl>
> | > | Subject: Re: System.IO.IOException: Cannot create a file when that
> file
> | > already exists.
> | > | Date: Thu, 7 Aug 2003 08:11:44 -0400
> | > | Lines: 78
> | > | X-Priority: 3
> | > | X-MSMail-Priority: Normal
> | > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
> | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
> | > | Message-ID: <exrIT0NXDHA.1204@.TK2MSFTNGP12.phx.gbl>
> | > | Newsgroups: microsoft.public.dotnet.framework.aspnet
> | > | NNTP-Posting-Host: 12.108.244.117
> | > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
> | > | Xref: cpmsftngxa06.phx.gbl
> | microsoft.public.dotnet.framework.aspnet:165699
> | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
> | > |
> | > | Thanks for the response.
> | > |
> | > | I understand that, but I'm trying to create a directory on the local
> | > drive.
> | > | This isn't a mapped share. The drive is physically present on the
> web
> | > | server, and if that web server fails, the other system physically
> takes
> | > over
> | > | the drive (ie, a cluster). I've also added the local ASPNET account
> to
> | > the
> | > | NTFS permissions on the folder, and it still fails.
> | > |
> | > | - Chad
> | > |
> | > |
> | > | "[MSFT]Allen" <yweng@.online.microsoft.com> wrote in message
> | > | news:j1MRYHMXDHA.2108@.cpmsftngxa06.phx.gbl...
> | > | > I agree with Natty. The problem you are having is most likely
caused
> | by
> | > | > having no right to access network resources. Willy address a
similar
> | > | > problem before. See
> | > | >
> | > |
> | >
> |
http://groups.google.com/groups?q=S...et&start=10&hl=
> | > | >
> en&lr=&ie=UTF-8&oe=UTF-8&selm=OELVHLBNCHA.2460%40tkmsftngp04&rnum=20.
> | > | >
> | > | > HTH,
> | > | > -Allen
> | > | >
> | > | > Disclaimer:
> | > | > This posting is provided "AS IS" with no warranties, and confers
no
> | > | rights.
> | > | > Got .Net? http://www.gotdotnet.com
> | > | >
> | > | > -------
> | > | > | From: "Chad Crowder" <chad.crowder@.gis.leica-geosystems.com>
> | > | > | Subject: System.IO.IOException: Cannot create a file when that
> file
> | > | > already exists.
> | > | > | Date: Wed, 6 Aug 2003 17:08:27 -0400
> | > | > | Lines: 29
> | > | > |
> | > | > | Getting the following error on my production server whether the
> file
> | > | > exists
> | > | > | or not:
> | > | > |
> | > | > | "System.IO.IOException: Cannot create a file when that file
> already
> | > | > exists."
> | > | > |
> | > | > | Here's the code generating the error (seems to be happening when
I
> | try
> | > | > | creating a directory)
> | > | > |
> | > | > | If dirmgr.Exists("s:\blah\" & txt_name.Text) Then
> | > | > | lblerror.Text = lblerror.Text & "Unable to build
physical
> | > path.
> | > | "
> | > | > &
> | > | > | txt_name.Text & " & Contact Dev Team.<BR>"
> | > | > | Else
> | > | > | dirmgr.CreateDirectory("s:\blah\" & txt_name.Text)
> | > | > |
> | > | > | This code runs fine on my development server (only the path is
> | > | > c:\blah\blah
> | > | > | instead of S). Also, my production server has failover cluser,
> and
> | > the
> | > | s
> | > | > | drive is the failover drive. I can't imagine that could be a
> | problem,
> | > | but
> | > | > | thought I would mention it.
> | > | > |
> | > | > | Asside from that, the only other thing I can think of is that
the
> | > | > framework
> | > | > | version on my dev box says 1.1, and the one on my production
> server
> | > | says:
> | > | > | 1.0.37 - surely that's not the cause is it? Permissions
shouldn't
> | be
> | > an
> | > | > | issue (i've even tried as domain admin)
> | > | > |
> | > | > | I would appreciate any help on this, as I'm out of ideas, and
can
> | find
> | > | no
> | > | > | other documentation on this.
> | > | > | - Chad
> | > | > |
> | > | > |
> | > | > |
> | > | >
> | > |
> | > |
> | > |
> | >
> |
> |
> |

System.IO.Path.GetTempFileName

Hi,
System.IO.Path.GetTempFileName( ) is supposed to create a temporary file and give us the path to the created file.
By default, where does this temp file get created? C:\Temp, C:\WinNT ?
Bcos right now this fails and error we get is "Access denied". But we dont know which folder's permissions to change as we dont know where this temp file is supposed to get created.

It will be in the ASPNET user's Temp Directory. For example, on mine:
C:\Documents and Settings\ComputerName\ASPNET\Local Settings\Temp


You are right ! I just found that out by using

tempPathString = Path.GetTempPath()
Thanks !


correctomundo!!
I know this is an old post, but System.IO.Path.GetTempPath() only puts stuff in the ASPNET user directory when the app runs on XP Pro. That is fine for development, but when the app is deployed to a Windows 2003 server the method returns C:\Windows\Temp. Calling GetTempFileName() creates a temp file there. Surely, it isn't safe or a best practice to write to the Windows directory, is it?

System.IO.Stream to resulting file.

I'm kicking myself as I've had this 'bug' before, and I cannot remember what it was that resolved it.

Essentially I'm re-writing my upload control, and its started exhibiting a weird 'corruption' thing.

Essentially I have this code:

Dim FileLenAs Integer = FileData.LengthDim FileDataByes(FileLen)As Byte' Initialize the stream.Dim ResourceStreamAs System.IO.Stream = FileData' Read the file into the byte array. ResourceStream.Read(FileDataByes, 0, FileLen)Dim SavingStreamAs System.IO.FileStream = _ System.IO.File.Create(Server.MapPath(Me.__VirtualPath & FileName))' Copy the byte array into a string.For iAs Integer = 0To FileLen - 1 SavingStream.WriteByte(FileDataByes(i).ToString())Next' Close the streams. ResourceStream.Close() SavingStream.Close()
FileData is a stream of the file, either from a server side file, or from a httppostedfile. The file appears to save ok, the size of the file is correct. And when checking the properties the byte count looks exactly the same.

But the file is quite obviously corrected, it wont open. I'm guessing the issue is to do with encoding. But I can't remember what I did to resolve it. I'm looking at my old framework and the code at this point is identical. Could it be something to do server settings changing the encoding of POSTS?

Pre-cheers.

Stevo.
Scratch that, its the literal 5 minutes after submitted a thread you realise your obvious mistake...

' Create the fragment.Dim FragmentAs String = _ Engine.Convert.ToHexString(FileData, 0, 2)
DOH! can't believe I forget to 'rewind' the stream after getting a fragment identifier...

Cheers anyways!

System.IO.StreamWriter -- Access to path is denied

Hi, I am trying to use the System.IO.StreamWriter to write to a file, and am getting an error that I have to set permissions for doing this in IIS -- I don't remember how to do this. Does anyone know how?

(Win XP Pro)


System.IO.StreamWriter writer = new System.IO.StreamWriter(@dotnet.itags.org."C:\NetApplicationTest",true);
Hi, u need to add aspNet User to the folder u trying to accessing and give the account have the permission to write/read it in the folder or file.

If u cant see any security tab on ur folder option, do tis below in any folder:
Tools>Folder Options>View>uncheck this "use simple file sharing(Recommended)"

then go to ur file's folder , note: folder
right click then properties> Security>Add > Advance> Find now>
Then u select ASPNET and add it, and set ASPNET to full control.

Hope tis help
life's Ng
beauty, thanks very much! -- I guess that I should study some XP Admin stuff... thanks 'gain ..

System.IO.StreamReader is not returning single quotes from my file

I am using a System.IO.StreamReader to read a text file that was created
with Notepad. It is returning everything except the single quotes (at least
that is the only thing I have noticed it is not returning). The code I am
using is:
Dim poemstreamreader As System.IO.StreamReader
poemstreamreader = System.IO.File.OpenText(Global.Poems(poemtitle))
cellPoemText.Text = poemstreamreader.ReadToEnd()
poemstreamreader.Close()
I know that it is reading from the correct file, because it is returning all
the characters correctly, I simply need to know how to make it not discard
special characters such as single quotes. How do I do this? Thanks.
--
Nathan Sokalski
njsokalski@dotnet.itags.org.hotmail.com
http://www.nathansokalski.com/Nathan,
You mean something as

> cellPoemText.Text = poemstreamreader.ReadToEnd()
> poemstreamreader.Close()
cellPoemText.Text = cellPoemText.Text.Replace("'","")
I hope this helps,
Cor
"Nathan Sokalski" <njsokalski@.hotmail.com> schrieb
>I am using a System.IO.StreamReader to read a text file that was
> created with Notepad. It is returning everything except the single quotes
> (at
> least that is the only thing I have noticed it is not returning).
> The code I am using is:
> Dim poemstreamreader As System.IO.StreamReader
> poemstreamreader =
> System.IO.File.OpenText(Global.Poems(poemtitle))
> cellPoemText.Text = poemstreamreader.ReadToEnd()
> poemstreamreader.Close()
>
> I know that it is reading from the correct file, because it is
> returning all the characters correctly, I simply need to know how to
> make it not discard special characters such as single quotes. How do
> I do this?
Don't know if this helps:
poemstreamreader = new io.streamreader(Global.Poems(poemtitle),
system.text.encoding.default)
This assumes ANSI encoding of the file.
Armin

Saturday, March 24, 2012

System.Net.WebClient

Hey all... i have to login a page and upload a file to that page ( cant
access to the code ) , Patrice and Kevin told me i could do it with
System.Net.WebClient, i could, but with a test site here in my intranet,
that website ( HTTPS ) detects i m not using the login form.. and i cant log
in that app...how can an ASP page detects if i login with the site or using
webclient ?
tnx..
CHIN@dotnet.itags.org.KD
www.racingclub.com.arGenerally the web app will look for a cookie in the request that was
issued during a succesful login (assuming the web app is using forms
authentication).
Some tips on how to programatically login:
http://odetocode.com/Articles/162.aspx
Scott
http://www.OdeToCode.com/blogs/scott/
On Tue, 1 Feb 2005 09:22:58 -0300, "CHIN@.KD"
<racingnet_ac@.yahoo.com.ar> wrote:

>Hey all... i have to login a page and upload a file to that page ( cant
>access to the code ) , Patrice and Kevin told me i could do it with
>System.Net.WebClient, i could, but with a test site here in my intranet,
>that website ( HTTPS ) detects i m not using the login form.. and i cant lo
g
>in that app...how can an ASP page detects if i login with the site or using
>webclient ?
>tnx..

System.Net.WebClient

Hey all... i have to login a page and upload a file to that page ( cant
access to the code ) , Patrice and Kevin told me i could do it with
System.Net.WebClient, i could, but with a test site here in my intranet,
that website ( HTTPS ) detects i m not using the login form.. and i cant log
in that app...how can an ASP page detects if i login with the site or using
webclient ?

tnx..

--
CHIN@dotnet.itags.org.KD
www.racingclub.com.arGenerally the web app will look for a cookie in the request that was
issued during a succesful login (assuming the web app is using forms
authentication).

Some tips on how to programatically login:
http://odetocode.com/Articles/162.aspx

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 1 Feb 2005 09:22:58 -0300, "CHIN@.KD"
<racingnet_ac@.yahoo.com.ar> wrote:

>Hey all... i have to login a page and upload a file to that page ( cant
>access to the code ) , Patrice and Kevin told me i could do it with
>System.Net.WebClient, i could, but with a test site here in my intranet,
>that website ( HTTPS ) detects i m not using the login form.. and i cant log
>in that app...how can an ASP page detects if i login with the site or using
>webclient ?
>tnx..

System.Net.WebClient and UploadFile() Problem

I am using WebClient to upload a file to a remote server. The call I'm
making looks as follows:
webClient.UploadFile("http://sdtpcal.someserver.com/Federation/Databases/Fed
eration.Zip",
@dotnet.itags.org."C:\Temp\Federation.Zip");
'Federation' is a virtual directory on the server I'm attempting to upload
the file to. The URL is valid and the file I'm attempting to transfer
exists. I keep getting 404 errors (not found). It's not clear to me exactly
what cannot be found. If I attempt to download this exact file from the same
exact URL using webClient.DownloadFile, it works fine!
Thanks for the help - Amos.You can not simply upload file on a server.
Something should wait for that file at the server end.
So url http://sdtpcal.someserver.com/Feder.../Federation.Zip to
download file looks correct.
but to upload you must write an ASPX page that will accept file and save to
the folder on a server.
George.
"Amos Soma" <amos_j_soma@.yahoo.com> wrote in message
news:OqGt9oK7GHA.1248@.TK2MSFTNGP03.phx.gbl...
>I am using WebClient to upload a file to a remote server. The call I'm
>making looks as follows:
> webClient.UploadFile("http://sdtpcal.someserver.com/Federation/Databases/F
ederation.Zip",
> @."C:\Temp\Federation.Zip");
> 'Federation' is a virtual directory on the server I'm attempting to upload
> the file to. The URL is valid and the file I'm attempting to transfer
> exists. I keep getting 404 errors (not found). It's not clear to me
> exactly what cannot be found. If I attempt to download this exact file
> from the same exact URL using webClient.DownloadFile, it works fine!
> Thanks for the help - Amos.
>
Thus wrote George Ter-Saakov,

> You can not simply upload file on a server.
> Something should wait for that file at the server end.
> So url
> http://sdtpcal.someserver.com/Feder.../Federation.Zip to
> download file looks correct.
> but to upload you must write an ASPX page that will accept file and
> save to the folder on a server.
You only need a web application endpoint for POST requests. If the OP can
use PUT, the web server may support this without any special user code (IIS
does for sure).
Both WebClient.UploadFile(uri, "PUT", fileName) and WebClient.OpenWrite(uri,
"PUT") will do the trick.
Cheers,
--
Joerg Jooss
news-reply@.joergjooss.de

System.Net.WebClient and UploadFile() Problem

I am using WebClient to upload a file to a remote server. The call I'm
making looks as follows:

webClient.UploadFile("http://sdtpcal.someserver.com/Federation/Databases/Federation.Zip",
@dotnet.itags.org."C:\Temp\Federation.Zip");

'Federation' is a virtual directory on the server I'm attempting to upload
the file to. The URL is valid and the file I'm attempting to transfer
exists. I keep getting 404 errors (not found). It's not clear to me exactly
what cannot be found. If I attempt to download this exact file from the same
exact URL using webClient.DownloadFile, it works fine!

Thanks for the help - Amos.You can not simply upload file on a server.
Something should wait for that file at the server end.

So url http://sdtpcal.someserver.com/Feder.../Federation.Zip to
download file looks correct.

but to upload you must write an ASPX page that will accept file and save to
the folder on a server.

George.

"Amos Soma" <amos_j_soma@.yahoo.comwrote in message
news:OqGt9oK7GHA.1248@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

>I am using WebClient to upload a file to a remote server. The call I'm
>making looks as follows:
>
webClient.UploadFile("http://sdtpcal.someserver.com/Federation/Databases/Federation.Zip",
@."C:\Temp\Federation.Zip");
>
'Federation' is a virtual directory on the server I'm attempting to upload
the file to. The URL is valid and the file I'm attempting to transfer
exists. I keep getting 404 errors (not found). It's not clear to me
exactly what cannot be found. If I attempt to download this exact file
from the same exact URL using webClient.DownloadFile, it works fine!
>
Thanks for the help - Amos.
>
>


Thus wrote George Ter-Saakov,

Quote:

Originally Posted by

You can not simply upload file on a server.
Something should wait for that file at the server end.
So url
http://sdtpcal.someserver.com/Feder.../Federation.Zip to
download file looks correct.
>
but to upload you must write an ASPX page that will accept file and
save to the folder on a server.


You only need a web application endpoint for POST requests. If the OP can
use PUT, the web server may support this without any special user code (IIS
does for sure).

Both WebClient.UploadFile(uri, "PUT", fileName) and WebClient.OpenWrite(uri,
"PUT") will do the trick.

Cheers,
--
Joerg Jooss
news-reply@.joergjooss.de