本文整理汇总了C#中StreamWriter.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# StreamWriter.Dispose方法的具体用法?C# StreamWriter.Dispose怎么用?C# StreamWriter.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StreamWriter
的用法示例。
在下文中一共展示了StreamWriter.Dispose方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteFile
//生成HTML页
public static bool WriteFile(string body, string fileName)
{
string path = HttpContext.Current.Server.MapPath("/"); //生成文件的存放目录
Encoding code = Encoding.GetEncoding("UTF-8");
StreamWriter sw = null;
string htmlfilename = fileName + ".html";
// 写文件
try
{
sw = new StreamWriter(path + htmlfilename, false, code);
sw.Write(body);
sw.Flush();
}
catch
{
return false;
}
finally
{
sw.Close();
sw.Dispose();
HttpContext.Current.Response.Write("恭喜 <a href=\"/" + htmlfilename + "\" target=\"_blank\">" + htmlfilename + "</a> 已经生成成功!<br />");
}
return true;
}
示例2: ControlFile
// カメラなどの座標が書き込まれた行を力技で強引に編集
void ControlFile(int n)
{
string tmpFile = Path.GetTempFileName();
using (StreamReader sr = new StreamReader(PATH))
using (StreamWriter sw = new StreamWriter(tmpFile))
{
int ReferLine = 0; // 参照する行
while (sr.Peek() > -1)
{
string line = sr.ReadLine(); // 読み込んだ一行
++ReferLine;
if (1 == ReferLine)
{
sw.Write(n);
}
else if (n + 3 == ReferLine)
{
sw.Dispose();
}
else
{
sw.Write("\n" + line);
}
//ReferLine++;
}
//閉じる
sr.Close();
sw.Close();
}
//入れ替え
File.Copy(tmpFile, PATH, true);
File.Delete(tmpFile);
}
示例3: Main
static void Main()
{
string fileName = "matrix.txt";
StreamReader streamReader = new StreamReader(fileName);
int length = int.Parse(streamReader.ReadLine());
int[,] array = new int[length, length];
for (int i = 0; i < array.GetLength(0); i++)
{
string[] line = streamReader.ReadLine().Split(' ');
for (int j = 0; j < array.GetLength(1); j++)
{
array[i, j] = int.Parse(line[j]);
}
}
streamReader.Dispose();
int bestSum = int.MinValue;
for (int i = 0; i < array.GetLength(0) - 1; i++)
{
for (int j = 0; j < array.GetLength(1) - 1; j++)
{
int currentSum = array[i, j] + array[i, j + 1] + array[i + 1, j] + array[i + 1, j + 1];
if (currentSum > bestSum)
{
bestSum = currentSum;
}
}
}
string resultFileName = "result.txt";
StreamWriter streamWriter = new StreamWriter(resultFileName);
streamWriter.Write(bestSum);
streamWriter.Dispose();
}
示例4: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
fileUrl = Server.MapPath(".") + "\\patch_Data\\update.sql";
bool done = bool.Parse(Setup.Patch.Get("db_updated"));
btnRunSQL.Enabled = !done;
lblDBCreation.Visible = done;
lnkNext.Visible = done;
if (!done)
{
string templateUrl = Server.MapPath(".") + "\\patch_Data\\update_template.sql";
StreamReader sr = new StreamReader(templateUrl);
StreamWriter sw = new StreamWriter(fileUrl, false);
try
{
dbDataContext db = new dbDataContext();
int first_group = db.sub_units.First().id;
while (!sr.EndOfStream)
sw.WriteLine(sr.ReadLine().Replace("_REPLACE_", first_group.ToString()));
}
catch (Exception ex)
{
lblOutput.Text = "An error occured: " + ex.ToString();
lblOutput.CssClass = "error";
}
finally
{
sr.Close(); sr.Dispose();
sw.Close(); sw.Dispose();
}
}
}
示例5: Write
public static void Write(string name, string value)
{
var w = new StreamWriter(@"c:\debug.out", true);
var n = DateTime.Now;
w.WriteLine(n.ToShortDateString() + " " + n.ToShortTimeString() + " Name:" + name + " Value:" + value);
w.Close();
w.Dispose();
}
示例6: SaveText
public static void SaveText(string path, string text)
{
using (StreamWriter writer = new StreamWriter(path, false, Encoding.Unicode))
{
writer.WriteLine(text);
writer.Close();
writer.Dispose();
}
}
示例7: WriteFile
public static void WriteFile(string message)
{
var filename = Process.GetCurrentProcess().Id.ToString() + ".txt";
var stream = new FileStream(filename, FileMode.Append);
var writer = new StreamWriter(stream);
writer.WriteLine(message);
writer.Dispose();
stream.Dispose();
}
示例8: Main
static void Main()
{
string fileName = "file.txt";
StreamReader streamReader = new StreamReader(fileName);
string str = streamReader.ReadToEnd().Replace(" start ", " finish ");
streamReader.Dispose();
StreamWriter streamWriter = new StreamWriter(fileName);
streamWriter.Write(str);
streamWriter.Dispose();
}
示例9: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
if (Session["DownHtml"] == null)
{
Server.Transfer("~/Docentes/ListaEventos.aspx");
return;
}
dgEvento.DataSource = Session["DownHtml"] as DataTable;
dgEvento.DataBind();
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms, Encoding.UTF8);
HtmlTextWriter txtSaida = new HtmlTextWriter(sw);
try
{
//TODO: criar CSS e remover BrowserTop
txtSaida.Write("<html>\n");
//TODO: css faz diferença?
//txtSaida.Write("<head>\n\t<link href=\"http://localhost:1364/Facin/_layouts/css/exporthtml.css\" rel=\"stylesheet\" type=\"text/css\">\n</head>\n");
txtSaida.Write("<body>\n");
txtSaida.Write("<H1>\nEventos</H1>\n");
dgEvento.RenderControl(txtSaida);
txtSaida.Write("\n</body>\n</html>");
}
catch (Exception )
{
Response.Redirect("~/Default/Erro.aspx");
}
finally
{
sw.Dispose();//FIXMW: close ou dispose?
}
Response.AddHeader("Content-disposition",
"attachment; filename=eventos.html");
Response.ContentType = "text/html";
try
{
Response.BinaryWrite(ms.ToArray());
Response.End();
}
finally
{
ms.Close();
}
}
示例10: WriteMessage
public void WriteMessage(String msg)
{
lock(myLockObj)
{
StreamWriter writer;
StreamReader reader;
//Erease old Log file Lines
if (File.Exists(fileName))
{
FileInfo fi = new FileInfo(fileName);
if (fi.Length > 512000)
{
//delete top 10 lines
try
{
reader = new StreamReader(fileName);
writer = new StreamWriter("tmp" + fileName, true);
for (int i = 0; !reader.EndOfStream; i++)
{
if (i < 10)
reader.ReadLine();
else
{
writer.WriteLine(reader.ReadLine());
}
}
reader.Close();
reader.Dispose();
writer.Close();
File.Delete(fileName);
File.Move("tmp" + fileName, fileName);
}
catch
{
return;
}
}
}
try
{
writer = new StreamWriter(fileName, true);
writer.WriteLine(DateTime.Now.ToString() + " : " + msg);
writer.Flush();
writer.Close();
writer.Dispose();
}
catch
{
return;
}
}
}
示例11: ProcessPlainTextRequest
static void ProcessPlainTextRequest(TcpClient client)
{
NetworkStream stream = client.GetStream();
var writer = new StreamWriter(stream, Encoding.ASCII);
writer.WriteLine("HTTP/1.1 200 OK");
writer.WriteLine("Server: .NET Core");
writer.WriteLine("Content-Type: text/html; charset=UTF-8");
writer.WriteLine("Content-Length: 13");
writer.WriteLine("Connection: close");
writer.WriteLine();
writer.WriteLine("Hello, World!");
writer.Dispose();
}
示例12: Main
static void Main()
{
string fileName = "file.txt";
StreamReader streamReader = new StreamReader(fileName);
string text = streamReader.ReadToEnd();
streamReader.Dispose();
string pattern = @"\btest\w*\b";
Regex regex = new Regex(pattern);
text = regex.Replace(text,"");
StreamWriter streamWriter = new StreamWriter(fileName);
streamWriter.Write(text);
streamWriter.Dispose();
}
示例13: Main
static void Main()
{
string fileName1 = "file1.txt";
string fileName2 = "file2.txt";
string fileNameResult = "result.txt";
StreamReader streamReader = new StreamReader(fileName1);
StreamWriter streamWriter = new StreamWriter(fileNameResult);
streamWriter.WriteLine(streamReader.ReadToEnd());
streamReader.Dispose();
streamReader = new StreamReader(fileName2);
streamWriter.Write(streamReader.ReadToEnd());
streamReader.Dispose();
streamWriter.Dispose();
}
示例14: getCareEvents
public List<CareEvent> getCareEvents()
{
//HttpContext.Current.Response.WriteFile("events.txt");
StreamWriter SW = new StreamWriter(current.Server.MapPath("~/events.txt"));
foreach (CareEvent careEvent in careEvents)
{
SW.WriteLine(careEvent.Description);
}
SW.Flush();
SW.Close();
SW.Dispose();
return careEvents;
}
示例15: CreateCsv
public static string CreateCsv(DataTable dataTable, string fileName)
{
string folder = "csv";
string filePath = folder + @"\" + fileName;
string file = AppDomain.CurrentDomain.BaseDirectory + filePath;
FileStream fStream;
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + folder))
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + folder);
if (File.Exists(file))
fStream = File.Open(file, FileMode.Truncate, FileAccess.Write);
else
fStream = File.Open(file, FileMode.CreateNew, FileAccess.ReadWrite);
System.Text.UTF8Encoding enc = new UTF8Encoding(false);
StringBuilder sb = new StringBuilder();
for (int k = 0; k < dataTable.Columns.Count; k++)
{
//add separator header
sb.Append(dataTable.Columns[k].ColumnName.Replace(",", "") + ',');
}
//append new line
sb.Append("\r\n");
foreach (DataRow dr in dataTable.Rows)
{
foreach (DataColumn dc in dataTable.Columns)
{
//add separator
sb.Append(dr[dc].ToString().Replace(",", "") + ',');
}
//append new line
sb.Append("\r\n");
}
using (var writer = new StreamWriter(fStream, enc))
{
writer.Write(sb.ToString());
writer.Close();
writer.Dispose();
fStream.Close();
fStream.Dispose();
}
return file;
}