用流读写文本
本文关键字:文本 读写 | 更新日期: 2023-09-27 18:05:20
我得到了一个c# windows窗体应用程序,它从sharepoint读取文本文件,然后做一些修改,然后它应该更新。听起来很简单,但我有点失去了流/凭证的使用。到目前为止,我能够阅读没有任何问题(所以没有凭据问题),但当尝试这样做时:
System.IO.StreamWriter file2 = new System.IO.StreamWriter(s);
这是我遇到的错误:
代码:
- n{"流不可写。"}系统。异常{系统。ArgumentException}
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();
不可能写入响应流。这不是HTTP协议的工作方式:)
基本上你必须有"东西"(例如一个ashx处理程序)在你的WWW应用程序,你必须做一个HttpWebRequest,打开一个请求流和POST数据回服务器。