Writing a text file to the file system

G

Guest

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
 
G

Guest

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 said:
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




cwbp15 said:
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
 
K

Ken Cox [Microsoft MVP]

"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 said:
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 said:
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




cwbp15 said:
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
 
G

Guest

Thanks for the information on encrypting the passwords.

I've never tried DPAPI before.

cwbp

Ken Cox said:
"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 said:
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 said:
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




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
 
S

shiva_ananthoju

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top