Writing a text file to the file system

Ask a Question related to ASP.NET Security, Design and Development.

  1. #1

    Default Writing a text file to the file system

    Using Visual Studio C#

    When I ran the following code:

    System.IO;

    private void Button1_Click(object sender, System.EventArgs e)
    {
    //FileStream fs = File.Create(Server.MapPath("test.txt"));
    FileStream fs = File.Create("C:\\MYSAVEDFILES\\test.txt");
    StreamWriter sw = new StreamWriter(fs);
    sw.Write(TextBox1.Text);
    sw.Close();
    fs.Close();

    }

    I initially got an error message that "access was denied." The message
    suggested that I give ASP.NET user access rights/permissions to the folder.

    I then [manually] gave the logged in user write permission to the folder.

    Then, when I ran the above code, the text file was created.

    Is there another way [i.e., programatically using C#] to allow my code to
    write a text file to the file system without giving access rights to a user?
    Can the permissions be given to the app [the code] instead of to a user?

    Any suggestions would be appreciated.

    Thanks.

    bebop



    cwbp Guest

  2. Similar Questions and Discussions

    1. Writing a comma delimted text file using CFFILE
      Can someone please tell me what is the proper syntac for writing a comma delimited text file. In the CFSET tag when I name the variable for the...
    2. Creating a text file & writing to it
      I went through a bit of articles on how to retrieve variables from a .txt file from the local machine. But I want to know can we create a text file...
    3. Writing a Text File in Flash...
      http://www.multidmedia.com/software/flashstudio/features.php is a free (for non-commercial use) app that lets you save to text files.
    4. Writing to a csv text file
      Say I ran a query that pulled 100 records with 10 fields into a RecordSet I'll call "rsQryResult" How would I: 1. write this data to a .csv text...
    5. A failure occurred writing to the resources file. Access is denied. -- RESX file is locked? -- WHY?
      Hi. This is an error that comes up fairly regularly when trying to run the "Rebuild All" command in a Solution that contains more than one...
  3. #2

    Default Re: Writing a text file to the file system

    Are you using impersonation in your application? The app should use the
    ASP.NET process account's credentials unless you are impersonating.

    If there is confusion, always check the value of
    System.Security.Principal.WindowsIdentity.GetCurre nt().Name.

    It is also possible to impersonate in code or move the file writing code to
    a COM+ component running under a different identity, but that probably isn't
    needed here.

    Joe K.

    "cwbp" <cwbp@discussions.microsoft.com> wrote in message
    news:AF8E541D-0550-4ED7-A5A4-4958C01F2F66@microsoft.com...
    > Using Visual Studio C#
    >
    > When I ran the following code:
    >
    > System.IO;
    >
    > private void Button1_Click(object sender, System.EventArgs e)
    > {
    > //FileStream fs = File.Create(Server.MapPath("test.txt"));
    > FileStream fs = File.Create("C:\\MYSAVEDFILES\\test.txt");
    > StreamWriter sw = new StreamWriter(fs);
    > sw.Write(TextBox1.Text);
    > sw.Close();
    > fs.Close();
    >
    > }
    >
    > I initially got an error message that "access was denied." The message
    > suggested that I give ASP.NET user access rights/permissions to the
    > folder.
    >
    > I then [manually] gave the logged in user write permission to the folder.
    >
    > Then, when I ran the above code, the text file was created.
    >
    > Is there another way [i.e., programatically using C#] to allow my code to
    > write a text file to the file system without giving access rights to a
    > user?
    > Can the permissions be given to the app [the code] instead of to a user?
    >
    > Any suggestions would be appreciated.
    >
    > Thanks.
    >
    > bebop
    >
    >
    >

    Joe Kaplan \(MVP - ADSI\) Guest

  4. #3

    Default Re: Writing a text file to the file system

    >> ASP.NET process account's

    Could you explain exactly how one uses this. How does one get get his hands
    on this, programatically?

    Thanks.



    "Joe Kaplan (MVP - ADSI)" wrote:
    > Are you using impersonation in your application? The app should use the
    > ASP.NET process account's credentials unless you are impersonating.
    >
    > If there is confusion, always check the value of
    > System.Security.Principal.WindowsIdentity.GetCurre nt().Name.
    >
    > It is also possible to impersonate in code or move the file writing code to
    > a COM+ component running under a different identity, but that probably isn't
    > needed here.
    >
    > Joe K.
    >
    > "cwbp" <cwbp@discussions.microsoft.com> wrote in message
    > news:AF8E541D-0550-4ED7-A5A4-4958C01F2F66@microsoft.com...
    > > Using Visual Studio C#
    > >
    > > When I ran the following code:
    > >
    > > System.IO;
    > >
    > > private void Button1_Click(object sender, System.EventArgs e)
    > > {
    > > //FileStream fs = File.Create(Server.MapPath("test.txt"));
    > > FileStream fs = File.Create("C:\\MYSAVEDFILES\\test.txt");
    > > StreamWriter sw = new StreamWriter(fs);
    > > sw.Write(TextBox1.Text);
    > > sw.Close();
    > > fs.Close();
    > >
    > > }
    > >
    > > I initially got an error message that "access was denied." The message
    > > suggested that I give ASP.NET user access rights/permissions to the
    > > folder.
    > >
    > > I then [manually] gave the logged in user write permission to the folder.
    > >
    > > Then, when I ran the above code, the text file was created.
    > >
    > > Is there another way [i.e., programatically using C#] to allow my code to
    > > write a text file to the file system without giving access rights to a
    > > user?
    > > Can the permissions be given to the app [the code] instead of to a user?
    > >
    > > Any suggestions would be appreciated.
    > >
    > > Thanks.
    > >
    > > bebop
    > >
    > >
    > >
    >
    >
    >
    M Guest

  5. #4

    Default Re: Writing a text file to the file system

    Good information.
    It has heloped me
    Rath Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139