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?

>Does the file path have to be different?

Use MapPath page property to open/read file.

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

>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.

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();
            }
        }

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.

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();

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.