By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,992 Members | 934 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,992 IT Pros & Developers. It's quick & easy.

Writing a text file to the file system

P: n/a
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

Nov 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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/de...ersonation.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"cwbp15" <cw****@discussions.microsoft.com> wrote in message
news:5A**********************************@microsof t.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

Nov 19 '05 #2

P: n/a
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/de...ersonation.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"cwbp15" <cw****@discussions.microsoft.com> wrote in message
news:5A**********************************@microsof t.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


Nov 19 '05 #3

P: n/a
"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/de...SecNetHT08.asp

"cwbp" <cw**@discussions.microsoft.com> wrote in message
news:1B**********************************@microsof t.com...
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/de...ersonation.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"cwbp15" <cw****@discussions.microsoft.com> wrote in message
news:5A**********************************@microsof t.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
>
>
>



Nov 19 '05 #4

P: n/a
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/de...SecNetHT08.asp

"cwbp" <cw**@discussions.microsoft.com> wrote in message
news:1B**********************************@microsof t.com...
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/de...ersonation.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"cwbp15" <cw****@discussions.microsoft.com> wrote in message
news:5A**********************************@microsof t.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
>
>
>


Nov 19 '05 #5

P: n/a

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 = new
StreamWriter(fs,System.Text.Encoding.Default);
sw.Write("Shiva Test");
sw.Close();

Regards,
A. Shiva Kumar
--
shiva_ananthoju
------------------------------------------------------------------------
shiva_ananthoju's Profile: http://www.highdots.com/forums/m1256
View this thread: http://www.highdots.com/forums/t673445

Nov 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.