将 file.Close() 与 StreamWriter 一起使用

发布于 2024-11-16 05:16:57 字数 641 浏览 4 评论 0原文

我在尝试使用 file.Close 与 StreamWriter 在此方法中遇到问题,它似乎不喜欢它。有人可以演示如何做到这一点吗? (原因是,另一种方法访问正在使用的文件,因此无法访问,因为该文件仍在由另一种方法使用。)

到目前为止的代码:

private static void TrimColon()
{
    using (StreamWriter sw = File.AppendText(@"process_trimmed.lst"))
    {
        StreamReader sr = new StreamReader(@"process_trim.lst");
        string myString = "";
        while (!sr.EndOfStream)
        {

            myString = sr.ReadLine();
            int index = myString.LastIndexOf(":");
            if (index > 0)
                myString = myString.Substring(0, index);

            sw.WriteLine(myString);
        }
    }
}

I am having problems trying to use the file.Close with StreamWriter in this method, it doesn't seem to like it. Could someone demonstrate how this could be done. (The reason so, is that another method accesses a file being used and hence can't, because the file is still in use by the other method.)

Code so far:

private static void TrimColon()
{
    using (StreamWriter sw = File.AppendText(@"process_trimmed.lst"))
    {
        StreamReader sr = new StreamReader(@"process_trim.lst");
        string myString = "";
        while (!sr.EndOfStream)
        {

            myString = sr.ReadLine();
            int index = myString.LastIndexOf(":");
            if (index > 0)
                myString = myString.Substring(0, index);

            sw.WriteLine(myString);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃→可爱长大的 2024-11-23 05:16:57
private static void TrimColon(String inputFilePath, String outputFilePath)
{
    //Error checking file paths
    if (String.IsNullOrWhiteSpace(inputFilePath))
        throw new ArgumentException("inputFilePath");
    if (String.IsNullOrWhiteSpace(outputFilePath))
        throw new ArgumentException("outputFilePath");

    //Check to see if files exist? - Up to you, I would.

    using (var streamReader = File.OpenText(inputFilePath))
    using (var streamWriter = File.AppendText(outputFilePath))
    {
        var text = String.Empty;

        while (!streamReader.EndOfStream)
        {
            text = streamReader.ReadLine();

            var index = text.LastIndexOf(":");
            if (index > 0)
                text = text.Substring(0, index);

            streamWriter.WriteLine(text);
        }
    }
}
private static void TrimColon(String inputFilePath, String outputFilePath)
{
    //Error checking file paths
    if (String.IsNullOrWhiteSpace(inputFilePath))
        throw new ArgumentException("inputFilePath");
    if (String.IsNullOrWhiteSpace(outputFilePath))
        throw new ArgumentException("outputFilePath");

    //Check to see if files exist? - Up to you, I would.

    using (var streamReader = File.OpenText(inputFilePath))
    using (var streamWriter = File.AppendText(outputFilePath))
    {
        var text = String.Empty;

        while (!streamReader.EndOfStream)
        {
            text = streamReader.ReadLine();

            var index = text.LastIndexOf(":");
            if (index > 0)
                text = text.Substring(0, index);

            streamWriter.WriteLine(text);
        }
    }
}
橙味迷妹 2024-11-23 05:16:57

由于“using”语句,StreamWriter 被关闭并刷新。所以不需要调用 close。

The StreamWriter is closed aswell as flushed due to the "using" statement. So no need to call close.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文