[英]How to open txt file on localhost and change is content
i want to open a css file using C# 4.5 and change only one file at a time. 我想使用C#4.5打开一个CSS文件,一次只更改一个文件。
Doing it like this gives me the exception - URI formats are not supported . 这样做会给我带来例外- 不支持URI格式 。
Can the line that I am looking and than start to insert text until cursor is pointing on some char ? 我正在寻找的行是否可以开始插入文本,直到光标指向某个字符?
public void ChangeColor() { string text = File.ReadAllText("http://localhost:8080/game/Css/style.css"); text = text.Replace("class='replace'", "new value"); File.WriteAllText("D://p.htm", text); }
I believe File.ReadAllText
is expecting a file path , not a URL. 我相信
File.ReadAllText
期望的是文件路径 ,而不是URL。
No, you cannot search/replace sections of a text file without reading and re-writing the whole file. 不,您不能在不读取和重写整个文件的情况下搜索/替换文本文件的各个部分。 It's just a text file, not a database.
它只是一个文本文件,而不是数据库。
most effective way to do it is to declare any control you want to alter the css of as "runat=server" and then modify the CssClass
property of it. 最有效的方法是将要更改其css的任何控件声明为“ runat = server”,然后修改其
CssClass
属性。 There is no known alternative way to modify the css file directly. 没有已知的直接修改css文件的替代方法。 Any other hacks is just that.. a hack and very innefficient way to do it.
任何其他骇客就是这样。.一种骇客,而且效率很低。
As mentioned before File.ReadAllText does not support url. 如前所述,File.ReadAllText不支持url。 Following is a working example with WebRequest:
以下是WebRequest的工作示例:
{
Uri uri = new Uri("http://localhost:8080/game/Css/style.css");
WebRequest req = WebRequest.Create(uri);
WebResponse web = req.GetResponse();
Stream stream = web.GetResponseStream();
string content = string.Empty;
using (StreamReader sr = new StreamReader(stream))
{
content = sr.ReadToEnd();
}
content.Replace("class='replace'", "new value");
using (StreamWriter sw = new StreamWriter("D://p.htm"))
{
sw.Write(content);
sw.Flush();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.