0

I got a C# windows forms app where it reads a text file from a sharepoint, then does some modification and then it should update. Sounds simple but I am kind of lost with the stream/credentials use. So far I am able to read without any problem (so there is no credentials problem) but when trying to do this:

System.IO.StreamWriter file2 = new System.IO.StreamWriter(s);

And this is the error I am having:

  • n {"Stream was not writable."} System.Exception {System.ArgumentException}

Code:

                        WebRequest request = WebRequest.Create(rutaCoinsDiscount);
                    request.Timeout = 30 * 60 * 1000;
                    request.UseDefaultCredentials = true;
                    request.Proxy.Credentials = request.Credentials;
                    WebResponse response = (WebResponse)request.GetResponse();
                    using (Stream s = response.GetResponseStream())

                        if (s != null)
                        {
                            string linea;
                            System.IO.StreamReader file = new System.IO.StreamReader(s);
                            while ((linea = file.ReadLine()) != null)
                            {
                                coinsLines[contador] = linea;
                                contador++;
                                if (linea != "") {
                                    lastline++;
                                }
                            }

                            file.Close();
                            int index2 = coinsLines[1].IndexOf(":") + 1;
                            string Gcoins = coinsLines[1].Substring(index2);
                            giveBalanceOld = Convert.ToInt32(Gcoins);

                            giveBalanceOld = giveBalanceOld - giveAmount;
                            coinsLines[1] = "GIVE:" + giveBalanceOld.ToString();

                            coinsLines[lastline] = DateTime.Today.ToString("d") + "+GIVE to:+" + destination + "+Coins: " + giveAmount;
                            System.IO.StreamWriter file2 = new System.IO.StreamWriter(s);
                            for (int j = 0; j < lastline; j++)
                            {
                                file2.WriteLine(coinsLines[j]);
                            }
                            file2.Close();
Ron Beyer
  • 11,003
  • 1
  • 19
  • 37
dracebus
  • 21
  • 7
  • 1
    As your error says, web response streams are not writable, you would need to upload the file back into sharepoint after modifying it, you can't just modify the stream. – Ron Beyer Jul 29 '15 at 19:15
  • Hi Ron, could you suggest how to do it? What should I do before System.IO.streamwrite file2 ... – dracebus Jul 29 '15 at 19:19
  • http://stackoverflow.com/questions/468469/how-do-you-upload-a-file-to-a-document-library-in-sharepoint or google "C# upload file to sharepoint" and find the one that works for you. – Ron Beyer Jul 29 '15 at 19:20
  • I have a lot of trouble with that, so what you are saying is that the best way to solve this is to CREATE a new file after modification and then UPLOADING it to the sharepoint. Is that so? – dracebus Jul 29 '15 at 20:51
  • Yes, the basics are you can't **write** to a **response** stream. You don't need to actually create a file, but you do need to establish a new request and upload the information back. Web based streams are typically one-way (with the exception of web sockets), so you can either read from them, or write to them, but not both. The stream created through a web request (which returns a response) isn't writable. – Ron Beyer Jul 29 '15 at 20:57
  • Thanks Ron, so I tried to create a new webrequest and then just write, but it did not work , any idea why not? code as follows – dracebus Jul 29 '15 at 23:55
  • `WebRequest request2 = WebRequest.Create(rutaCoinsDiscount); request2.Timeout = 30 * 60 * 1000; request2.UseDefaultCredentials = true; request2.Proxy.Credentials = request.Credentials; WebResponse response2 = (WebResponse)request.GetResponse(); using (Stream s2 = response.GetResponseStream())` – dracebus Jul 29 '15 at 23:56
  • `if (s2 != null){ System.IO.StreamWriter file2 = new System.IO.StreamWriter(s2); for (int j = 0; j < lastline; j++) file2.WriteLine(coinsLines[j]); file2.Close();}` – dracebus Jul 29 '15 at 23:57
  • I really don't want to sound like an ass here but I'm not sure how many times I can say you can't write to a response stream. Check the Google links for uploading to SharePoint, they are pretty clear. – Ron Beyer Jul 30 '15 at 00:43

1 Answers1

2

It is impossible to write to the Response Stream. This is not how the HTTP protocol works :)

Basically you'll have to have "something" (an ashx handler for example) in your WWW app and you have to make a HttpWebRequest, open a Request Stream and POST the data back to the server.

Jan W
  • 464
  • 4
  • 8
  • I tried doing a second webrequest at the end, but no luck either :( `WebRequest request2 = WebRequest.Create(rutaCoinsDiscount); request2.Timeout = 30 * 60 * 1000; request2.UseDefaultCredentials = true; request2.Proxy.Credentials = request.Credentials; WebResponse response2 = (WebResponse)request.GetResponse(); using (Stream s2 = response.GetResponseStream()) if (s2 != null){System.IO.StreamWriter file2 = new System.IO.StreamWriter(s2); for (int j = 0; j < lastline; j++) file2.WriteLine(coinsLines[j]); file2.Close();}` – dracebus Jul 29 '15 at 19:29