当前位置: 首页>>代码示例>>C#>>正文


C# StreamWriter.Dispose方法代码示例

本文整理汇总了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;
    }
开发者ID:uvbs,项目名称:taobaoke,代码行数:27,代码来源:htm.aspx.cs

示例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);
 }
开发者ID:backspace229,项目名称:NUIMolecularModel,代码行数:34,代码来源:PrintPosition.cs

示例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();
 }
开发者ID:kalinnikol,项目名称:TelerikAcademy-1,代码行数:32,代码来源:SquareMatrix.cs

示例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();
         }
     }
 }
开发者ID:EdiCarlos,项目名称:MyPractices,代码行数:31,代码来源:update_database.aspx.cs

示例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();
 }
开发者ID:adamclerk,项目名称:Rainmeter-Svn-Plugin,代码行数:8,代码来源:Debug.cs

示例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();
     }
 }
开发者ID:ATouhou,项目名称:QuranCode,代码行数:9,代码来源:PublicStorage.cs

示例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();
 }
开发者ID:motouhui,项目名称:3in1,代码行数:9,代码来源:RitualBeattleScript.cs

示例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();
 }
开发者ID:kalinnikol,项目名称:TelerikAcademy-1,代码行数:10,代码来源:ReplaceSubstring.cs

示例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();
        }
    }
开发者ID:marlonps,项目名称:OpenSARC,代码行数:52,代码来源:DownloadHTML.aspx.cs

示例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;
         }
     }
 }
开发者ID:JDrosdeck,项目名称:2Sprout-Windows,代码行数:52,代码来源:ErrorLog.cs

示例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();
 }
开发者ID:axxu,项目名称:corefxlab,代码行数:13,代码来源:main.cs

示例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();
 }
开发者ID:kalinnikol,项目名称:TelerikAcademy-1,代码行数:13,代码来源:DeleteWordsWithPrefix.cs

示例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();
 }
开发者ID:kalinnikol,项目名称:TelerikAcademy-1,代码行数:14,代码来源:ConcatenateTwoTextFiles.cs

示例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;
    }
开发者ID:codehub,项目名称:839275,代码行数:15,代码来源:WallController.cs

示例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;
    }
开发者ID:asrasalman,项目名称:Auto-Price-Manager,代码行数:48,代码来源:Export.cs


注:本文中的StreamWriter.Dispose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。