Writing a text file to the file system

Discussion in 'ASP .Net' started by =?Utf-8?B?Y3dicDE1?=, Mar 21, 2005.

  1. 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
     
    =?Utf-8?B?Y3dicDE1?=, Mar 21, 2005
    #1
    1. Advertising

  2. Yes, you can use impersonation.

    For example, you can add a line similar to this to your web.config file:
    <identity impersonate="true" userName="domain\MyUserName">
    password="password"/>

    Here's more info on impersonation:
    http://msdn.microsoft.com/library/d...-us/cpguide/html/cpconaspnetimpersonation.asp

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    http://SteveOrr.net


    "cwbp15" <> wrote in message
    news:...
    > 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
    >
    >
    >
     
    Steve C. Orr [MVP, MCSD], Mar 21, 2005
    #2
    1. Advertising

  3. That helped. Thanks Steve.

    I noticed the password for my currently logged in user is in clear text when
    using impersonation.

    Added this line to the Application's web.config file

    <identity impersonate="true" userName="username" password="password"/>

    Wondering if there's a way to use impersonation and have the password
    encrypted or placed elsewhere.

    cwbp
    "Steve C. Orr [MVP, MCSD]" wrote:

    > Yes, you can use impersonation.
    >
    > For example, you can add a line similar to this to your web.config file:
    > <identity impersonate="true" userName="domain\MyUserName">
    > password="password"/>
    >
    > Here's more info on impersonation:
    > http://msdn.microsoft.com/library/d...-us/cpguide/html/cpconaspnetimpersonation.asp
    >
    > --
    > I hope this helps,
    > Steve C. Orr, MCSD, MVP
    > http://SteveOrr.net
    >
    >
    > "cwbp15" <> wrote in message
    > news:...
    > > 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
    > >
    > >
    > >

    >
    >
    >
     
    =?Utf-8?B?Y3dicA==?=, Mar 22, 2005
    #3
  4. "Summary: This How To shows you how to use DPAPI from an ASP.NET Web
    application or Web service to encrypt sensitive data. (7 printed pages)"

    Ken


    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT08.asp

    "cwbp" <> wrote in message
    news:...
    > That helped. Thanks Steve.
    >
    > I noticed the password for my currently logged in user is in clear text
    > when
    > using impersonation.
    >
    > Added this line to the Application's web.config file
    >
    > <identity impersonate="true" userName="username" password="password"/>
    >
    > Wondering if there's a way to use impersonation and have the password
    > encrypted or placed elsewhere.
    >
    > cwbp
    > "Steve C. Orr [MVP, MCSD]" wrote:
    >
    >> Yes, you can use impersonation.
    >>
    >> For example, you can add a line similar to this to your web.config file:
    >> <identity impersonate="true" userName="domain\MyUserName">
    >> password="password"/>
    >>
    >> Here's more info on impersonation:
    >> http://msdn.microsoft.com/library/d...-us/cpguide/html/cpconaspnetimpersonation.asp
    >>
    >> --
    >> I hope this helps,
    >> Steve C. Orr, MCSD, MVP
    >> http://SteveOrr.net
    >>
    >>
    >> "cwbp15" <> wrote in message
    >> news:...
    >> > 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
    >> >
    >> >
    >> >

    >>
    >>
    >>
     
    Ken Cox [Microsoft MVP], Mar 22, 2005
    #4
  5. Thanks for the information on encrypting the passwords.

    I've never tried DPAPI before.

    cwbp

    "Ken Cox [Microsoft MVP]" wrote:

    > "Summary: This How To shows you how to use DPAPI from an ASP.NET Web
    > application or Web service to encrypt sensitive data. (7 printed pages)"
    >
    > Ken
    >
    >
    > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT08.asp
    >
    > "cwbp" <> wrote in message
    > news:...
    > > That helped. Thanks Steve.
    > >
    > > I noticed the password for my currently logged in user is in clear text
    > > when
    > > using impersonation.
    > >
    > > Added this line to the Application's web.config file
    > >
    > > <identity impersonate="true" userName="username" password="password"/>
    > >
    > > Wondering if there's a way to use impersonation and have the password
    > > encrypted or placed elsewhere.
    > >
    > > cwbp
    > > "Steve C. Orr [MVP, MCSD]" wrote:
    > >
    > >> Yes, you can use impersonation.
    > >>
    > >> For example, you can add a line similar to this to your web.config file:
    > >> <identity impersonate="true" userName="domain\MyUserName">
    > >> password="password"/>
    > >>
    > >> Here's more info on impersonation:
    > >> http://msdn.microsoft.com/library/d...-us/cpguide/html/cpconaspnetimpersonation.asp
    > >>
    > >> --
    > >> I hope this helps,
    > >> Steve C. Orr, MCSD, MVP
    > >> http://SteveOrr.net
    > >>
    > >>
    > >> "cwbp15" <> wrote in message
    > >> news:...
    > >> > 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
    > >> >
    > >> >
    > >> >
    > >>
    > >>
    > >>

    >
    >
     
    =?Utf-8?B?Y3dicA==?=, Mar 22, 2005
    #5
  6. This will work and u don't need to give permissions to any folder!!!

    string strFilePath = "C:\\Test.txt";
    FileStream fs = new FileStream(strFilePath,FileMode.Append);
    StreamWriter sw = ne
    StreamWriter(fs,System.Text.Encoding.Default);
    sw.Write("Shiva Test");
    sw.Close();

    Regards,
    A. Shiva Kuma

    --
    shiva_ananthoj
    -----------------------------------------------------------------------
    shiva_ananthoju's Profile: http://www.highdots.com/forums/m125
    View this thread: http://www.highdots.com/forums/t67344
     
    shiva_ananthoju, Oct 29, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BLF
    Replies:
    4
    Views:
    1,282
    Mike Treseler
    Aug 7, 2004
  2. HNguyen
    Replies:
    4
    Views:
    2,408
    HNguyen
    Dec 21, 2004
  3. =?Utf-8?B?Y3dicA==?=

    Writing a text file to the file system

    =?Utf-8?B?Y3dicA==?=, Mar 21, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    322
    =?Utf-8?B?Y3dicA==?=
    Mar 21, 2005
  4. icedragon
    Replies:
    0
    Views:
    649
    icedragon
    Jul 13, 2010
  5. cwbp

    Writing a text file to the file system

    cwbp, Mar 21, 2005, in forum: ASP .Net Security
    Replies:
    2
    Views:
    192
Loading...

Share This Page