tags:

views:

24

answers:

4

Hi,

So I have a local WPF web browser application and I need it to write to a text file. However I always get the an exception of type System.Reflection.TargetInvocationException. I don't get any more information but I'm pretty sure it is because it is being run from a web browser (security etc.).

The code I am using works in a regular windows form application etc. it is the usual:

        StreamWriter sw;
        sw = File.CreateText("c:\\myTextFile.txt");
        sw.WriteLine("First line");
        sw.Close();

So, is there a workaround for this?

Many thanks, Thomas

A: 

You could try running in Full Trust mode.

Darin Dimitrov
A: 

WPF in a browser is going to default to partial trust. You can change that in the Security tab in Project Properties (subject, of course, to it being installed by a trusted user).

ThatSteveGuy
A: 

The issue is not WPF security problems. The issue is an operating system security feature in play: you can't write to the root of the C: drive without admin privs. Not only would your app need to be in WPF Full Trust mode, but you'd need to grant your app administrative priveledges on Vista and Windows 7 and beyond.

All that said, you should be able to write to files in non-protected paths. For example, you could write to a temporary file as returned by Path.GetTempFilePath(). However, it's likely your WPF browser app will still have to be running in full trust to write to disk at all.

Judah Himango
A: 

See this page, describing Partial Trust security considerations. If you need to store the file for your own application's use, consider using Isolated Storage. If you want to allow the user to save a file to disk, you can use SaveFileDialog.OpenFile to allow the user to choose a path and confirm saving the file.

Dan Bryant