473,499 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Nov 19 '05 #1
5 3090
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
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
"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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6836
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
5
3143
by: Iceman | last post by:
I am logging things to a file but when I kill the app and restart it, all the other logging is gone. How can I prevent this form happening?
2
6766
by: simonc | last post by:
Is there an easy way of writing a number in 32 bit integer format into four bytes of a file? I am experimenting with FilePut but I can only make it work by defining a four byte array and doing some...
2
4324
by: Richard Thornley | last post by:
Hello, I am working on a VB.Net application and I am getting an error ONLY on the main computer that uses the application. The main computer is on the other coast so of course it would be the...
16
2181
by: iwdu15 | last post by:
how can i open a file i saved and place the info into different text boxes?
2
1621
by: darrel | last post by:
Our production web site went down today. We discovered that it was because the XML file we use to create the site navigation (most all of the controls on every page rely on this XML file) was only...
3
2147
by: bbepristis | last post by:
Hey all I have this code that reads from one text file writes to another unless im on a certian line then it writes the new data however it only seems to do about 40 lines then quits and I cant...
2
3023
by: Craig | last post by:
I have the need to write a byte of information to a specific location in a text file. eg. the file looks something like this. FYYNN Line 1 Line 2 <eof>
7
1629
by: tshad | last post by:
I have a program in 2005 that is reading a text file removing text and then writing it back out again. It removes lines that start with PRINT. This program has worked fine for months. Now all...
0
7174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6894
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7388
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.