如何解决此C#错误? [英] How do I fix this C# error?

查看:102
本文介绍了如何解决此C#错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个控制台应用程序,该应用程序创建一个文件夹并在其中存储文本文件.该程序将复制网站的HTML代码,并将其显示在控制台窗口中,然后再将代码复制到文本文件中.当我运行它时,它会显示 控制台上的HTML代码,在指定位置创建文件夹和文本文件.但是,我遇到的问题是,当它将HTML代码复制到文本文件时,会弹出错误消息

I am attempting to create a console application that creates a folder and stores a text file in it. The program will copy the HTML code of a website and display it in a console window, before copying the code onto the text file. WhenI run it, it displays the HTML code on the console, creates the folder and the text file in the specified location. However, the issue that I am having is that when it gets copying the HTML code to the text file, an error message pops up

说""类型为"System.IO.IOException"的未处理异常发生在mscorlib.dll中.附加信息:该进程无法访问文件C:\ Users \ Public \ Documents \ myDirectory \ myWebCode.txt,因为它正在被另一个进程使用". 如何解决此错误?

saying 'An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll Additional information: The process cannot access the file C:\Users\Public\Documents\myDirectory\myWebCode.txt' because it is being used by another process'. how do I fix this error?

下面是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;


namespace GetWebPageSourceCode
{
    class WebsiteCode
    {
        static void Main(string[] args)
        {
          Directory.CreateDirectory(@"C:\Users\Public\Documents\myDirectory");
          File.Create(@"C:\Users\Public\Documents\myDirectory\myWebCode.txt");
            
            //checks if the directory exists
            if(Directory.Exists(@"C:\Users\Public\Documents\myDirectory"))
            {
                //checks if the file existsn within the folder
                if(File.Exists(@"C:\Users\Public\Documents\myDirectory\myWebCode.txt"))
                {

                    WebClient client = new WebClient();

                     string reply = client.DownloadString("https://www.youtube.com/?gl=GB");

                     Console.WriteLine(reply);

                     File.WriteAllText(@"C:\Users\Public\Documents\myDirectory\myWebCode.txt", reply);

                     Console.ReadLine();
                }

            }
                     

        }

当我第一次运行程序时,错误消息弹出.第一次运行后,我注释掉了前两个代码(用于创建文件夹和文件),并且运行良好.这是从第一次编译创建文件和文件夹之后的结果.


When I ran the program first the error message popped up. I commented out the first two code (to create the folder and file) after the first run and it ran fine. This was after the file and folder were created from the first compiling.



推荐答案

为什么首先创建文件?使用File.WriteAllText时不需要.

why do you create the file first? Its not needed when using File.WriteAllText.

BTW由于发生File.Create行而发生错误(它返回FileStream并且文件被锁定,直到FileStream被关闭/处置).请参阅(尤其是该页面底部的示例):

BTW The error occurs due to the File.Create line (it returns a FileStream and the file is locked until the FileStream is closed/disposed). See (especially the example at the bottom of that page):

https://msdn.microsoft.com/zh-CN/library/d62kzs03(v = vs.110).aspx

https://msdn.microsoft.com/en-us/library/d62kzs03(v=vs.110).aspx

此致

  Thorsten

  Thorsten


这篇关于如何解决此C#错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆