Create file fails (StreamWriter )


S

Sonnich Jensen

Hi

This simple piece of code does not work

FileStream fs = new FileStream(s, FileMode.CreateNew, FileAccess.ReadWrite);
using (StreamWriter writer = new StreamWriter(fs))
{
writer.WriteLine("blah blah");
}

However, I need to write to the folder where my programme is (c:\prg files\my folder\ ) - and that is not allowed

I have surfed the net but I really cannot find a working solution

WBR
Sonnich
 
Ad

Advertisements

B

bradbury9

El martes, 25 de noviembre de 2014 15:19:49 UTC+1, Sonnich Jensen escribió:
Hi

This simple piece of code does not work

FileStream fs = new FileStream(s, FileMode.CreateNew, FileAccess.ReadWrite);
using (StreamWriter writer = new StreamWriter(fs))
{
writer.WriteLine("blah blah");
}

However, I need to write to the folder where my programme is (c:\prg files\my folder\ ) - and that is not allowed

I have surfed the net but I really cannot find a working solution

WBR
Sonnich
Not sure if you will see this link in english :)

Demanding file rights to write files: http://support.microsoft.com/kb/315529
 
S

Sonnich Jensen

El martes, 25 de noviembre de 2014 15:19:49 UTC+1, Sonnich Jensen escribió:

Not sure if you will see this link in english :)

Demanding file rights to write files: http://support.microsoft.com/kb/315529
Gracias, it came in English but I would manage somehow anyway.

But even that I manage to get those rights, it still gives the same exception when trying to create the file - I checked that I cannot seem to add my permission to the FileStream or so....

How can I connect those 2?
 
S

SMJT

Gracias, it came in English but I would manage somehow anyway.

But even that I manage to get those rights, it still gives the same exception when trying to create the file - I checked that I cannot seem to add my permission to the FileStream or so....

How can I connect those 2?
Change FileMode.CreateNew to FileMode.Create

When you run your code it will execute once and create the file (Check the folder), the second time it throws an exception because the file already exists, which is correct. Changing to FileMode.Create will force it to createa new file every time it executes.
 
S

Sonnich Jensen

Change FileMode.CreateNew to FileMode.Create

When you run your code it will execute once and create the file (Check the folder), the second time it throws an exception because the file already exists, which is correct. Changing to FileMode.Create will force it to create a new file every time it executes.
No, but it does get one step further ahead:


FileIOPermission writePermission = new FileIOPermission(FileIOPermissionAccess.AllAccess, Path.GetDirectoryName(Application.ExecutablePath));
try
{
writePermission.Demand();
}
catch(Exception ex)
{
}

// it used to crash here, but chaning createnew to create works
FileStream fs = new FileStream(s, FileMode.Create, FileAccess.Write);
// it still crashes here with the same unarthirized.....
using (StreamWriter writer = new StreamWriter(fs))
{
writer.WriteLine("whatever");
}

How do I now overcome the next step?
 
B

bradbury9

El miércoles, 26 de noviembre de 2014 13:13:29 UTC+1, Sonnich Jensen escribió:
No, but it does get one step further ahead:


FileIOPermission writePermission = new FileIOPermission(FileIOPermissionAccess.AllAccess, Path.GetDirectoryName(Application.ExecutablePath));
try
{
writePermission.Demand();
}
catch(Exception ex)
{
}

// it used to crash here, but chaning createnew to create works
FileStream fs = new FileStream(s, FileMode.Create, FileAccess.Write);
// it still crashes here with the same unarthirized.....
using (StreamWriter writer = new StreamWriter(fs))
{
writer.WriteLine("whatever");
}

How do I now overcome the next step?
You can do a "MessageBox.Show(ex.Message)", "Console.WriteLine(ex.Message)"or similar inside the catch to see if the writePermission.Demand() is failing. Or set a breakpoint if using a debugger.
 
Ad

Advertisements

S

Sonnich Jensen

El miércoles, 26 de noviembre de 2014 13:13:29 UTC+1, Sonnich Jensen escribió:

You can do a "MessageBox.Show(ex.Message)", "Console.WriteLine(ex.Message)" or similar inside the catch to see if the writePermission.Demand() is failing. Or set a breakpoint if using a debugger.
Yes, but it doesnt really explain why I get the error - I can demand the rights, and create the file stream, but the stream writer fails - this line

using (StreamWriter writer = new StreamWriter(fs))

error: System.UnauthorizedAccessException

I think I have the rights, but I still dont - somehow.
Any ideas on this?
 
Ad

Advertisements

B

bradbury9

El jueves, 27 de noviembre de 2014 08:54:13 UTC+1, Sonnich Jensen escribió:
Yes, but it doesnt really explain why I get the error - I can demand the rights, and create the file stream, but the stream writer fails - this line

using (StreamWriter writer = new StreamWriter(fs))

error: System.UnauthorizedAccessException

I think I have the rights, but I still dont - somehow.
Any ideas on this?
If you get UnauthorizedAccessException is because your user probably does not have rights to write to the folder. That is because of the way Windows handles specific folders. If I recall correctly is something callled UAC.

You have three options:
a) Compile the program, left click the executable, run as administrator.
b) Disable the UAC. Not recommended, it is a big security feature.
c) Write to other folder. There are specific folders where applications canwrite program related files without giving problems.

Check this link for the C option. http://stackoverflow.com/questions/1191941/file-write-permission-issue-under-program-files-folder
 

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

Top