0

Hi. I added a file called "sldata.txt" to my Silverlight solution (which I test in an ASP.NET site) in VS 2010. But when I try to access it using stream reader:

StreamReader sr = new StreamReader("sldata.txt");

I get a SecurityException - File operation not permitted. Access to path 'chatdata.txt' is denied. I get this even when trying to create a new file and even when I try to access a file that doens't even exist. What am I doing wrong here? I have given NETWORK SERVICE full permission to the file and I tried editing the web.config file in my ASP.NET site. Does the file path have to be different?

4
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by jaspreetsingh12
0

>Does the file path have to be different?

Use MapPath page property to open/read file.

StreamReader sr = new StreamReader(MapPath("~/sldata.txt"));
0

>Does the file path have to be different?

Use MapPath page property to open/read file.

StreamReader sr = new StreamReader(MapPath("~/sldata.txt"));

"The name MapPath does not exist in the current context"
What namespace do I have to include?? I think this only works on ASP.NET pages but I want to access it from my Silverlight app.

Edited by moni94: n/a

0

Sorry! I missed that you want to read a file from within SL app.

Firstly, move the text file sldata.txt into ClientBin folder and use WebClient's OpenReadAsync method to read a file.

private void Button_Click(object sender, RoutedEventArgs e)
        {
            System.Net.WebClient wc = new WebClient();
            wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
            wc.OpenReadAsync(new Uri("sample.txt", UriKind.Relative), "GET");
        }

        void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                txt1.Text = e.Error.Message;
            }
            else
            {
                System.IO.StreamReader sr = new System.IO.StreamReader(e.Result);
                txt1.Text = sr.ReadToEnd();
                sr.Close();
            }
        }
0

Sorry! I missed that you want to read a file from within SL app.

Firstly, move the text file sldata.txt into ClientBin folder and use WebClient's OpenReadAsync method to read a file.

private void Button_Click(object sender, RoutedEventArgs e)
        {
            System.Net.WebClient wc = new WebClient();
            wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
            wc.OpenReadAsync(new Uri("sample.txt", UriKind.Relative), "GET");
        }

        void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                txt1.Text = e.Error.Message;
            }
            else
            {
                System.IO.StreamReader sr = new System.IO.StreamReader(e.Result);
                txt1.Text = sr.ReadToEnd();
                sr.Close();
            }
        }

Thanks. I can now read from the file but I can't write:

wc.OpenWriteAsync(new Uri("sldata.txt", UriKind.Relative), "POST");
wc.OpenWriteCompleted += (object sender_, OpenWriteCompletedEventArgs e_) =>
          {
                  sw = new StreamWriter(e_.Result);
                  sw.Write(somestring);
                  sw.Flush();
          };

It doesn't give an error but it doesn't write to the file either.

Edited by moni94: n/a

0

use following code to write file. make sure SL application running in out of browser mode

string filePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),"Silverlight.txt");
            System.IO.StreamWriter file = new System.IO.StreamWriter(filePath, true);
            file.WriteLine(strMsg);
            file.Close();

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

0

Hello,
I am still getting File Operation Not Permitted, Access to path is denied error even while running Silverlight application is out of Browser mode if i use:

System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Silverlight.txt");

But yes it works for

System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Silverlight.txt");

Thanks

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.