File Permissions - Writing to File from Code

Discussion in 'ASP.NET 2.0' started by vvsharma, Sep 26, 2006.

  1. Use Server.Mappath while creating a file in your working directory.


    Sample Code.
    -------------



    <%@ Page Language="VB" Debug="true" %>


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


    <script language="vb" runat="Server">


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)


    Dim myFileStream As StreamWriter = Nothing


    Try


    myFileStream = File.CreateText(Server.MapPath(".") &amp; "/test.txt")


    myFileStream.WriteLine("HELLO")


    myFileStream.Close()


    Response.Write("File Successfully Created!")


    Catch exc As Exception


    Response.Write("Error in Creating file. Error is " &amp; exc.ToString())


    Finally


    If Not myFileStream Is Nothing Then


    myFileStream.Close()


    End If





    Hope this Helps!


    End Try





    End Sub


    </script>




    Vikram

    DiscountASP.NET
    www.DiscountASP.NET
     
  2. Hi Guys.


    I'm trying to use the following code to write to a file in a
    sub-directory on my site. But I keep getting permission denied errors.


    I've added <identity impersonate="true"/> to the web.config file.
    All this does is ask me for a username and password,
    I've tried using the username and passsword I use to
    access my site via ftp but am not sure what format to
    use for the username ... or even if I need to create a
    new user?


    Does someone need to set file permissions explicitly for me?


    Code below:
    -----------------------------------------------------
    Dim FileInfo As System.IO.FileInfo
    Dim Tex As System.IO.StreamWriter
    Dim str As String
    FileInfo = New System.IO.FileInfo("Test.txt")
    Tex = FileInfo.CreateText()
    str = "Test"
    Tex.WriteLine(str)
    Tex.Write(Tex.NewLine)
    Tex.Close()
    Response.Write("Text file created.")
    -----------------------------------------------------



    Please help.


    Many Thanks,
    Richard
     
  3. Thanks Vikram

    Your solution worked 100%

    Very Much Appreciated
    Richard
     

Share This Page