Writing a text file to the file system

C

cwbp

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
 
J

Joe Kaplan \(MVP - ADSI\)

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.GetCurrent().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.
 
M

M

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) said:
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.GetCurrent().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 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
 

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,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top