简体   繁体   English

C# ReadAllText 文件不存在

[英]C# ReadAllText File Doesn't Exist

I am creating a very basic program to try out some File options, in this case I want to count how many words are in a text file I have created with the help of ReadAllText, however, the path name always shows an exception.我正在创建一个非常基本的程序来尝试一些文件选项,在这种情况下,我想计算在 ReadAllText 的帮助下创建的文本文件中有多少个单词,但是,路径名总是显示异常。

This is my code:这是我的代码:

var path = @"‪‪C:\Users\pandrews\countme.docx";

            //Checks if file exists or not 
            if (File.Exists(path))
            {
                Console.WriteLine("File Exists");

                var content = File.ReadAllText(path);

                Console.WriteLine(content);

            } else {

                Console.WriteLine("File doesn't exist");

            }
            
            //Checks if directory exists, if it does it returns a list of files in the directory and shows on the console.

            if(Directory.Exists(@"C:\Users\pandrews"))
            {
                Console.WriteLine("Directory exists");

                var files = Directory.GetFiles(@"C:\Users\pandrews");

                foreach(var item in files)
                {
                    Console.WriteLine(item);
                }

            } else 
            {
                Console.WriteLine("Directory doesn't Exist");
            }

As you can see, I check if the file exists yet visual studio says it doesn't, however, if I check if the directory exists, it says it does and if I check the list of files it lists the file I want to read.如您所见,我检查文件是否存在,但visual studio 说它不存在,但是,如果我检查目录是否存在,它说它存在,如果我检查文件列表,它会列出我想阅读的文件.

C:\Users\pandrews\.gitconfig
C:\Users\pandrews\countme.docx
C:\Users\pandrews\NTUSER.DAT
C:\Users\pandrews\ntuser.dat.LOG1
C:\Users\pandrews\ntuser.dat.LOG2
C:\Users\pandrews\ntuser.ini

If I use a try catch, when trying to read all text it shows the following exception:如果我使用 try catch,则在尝试读取所有文本时会显示以下异常:

El nombre de archivo, el nombre de directorio o la sintaxis de la etiqueta del volumen no son correctos. : 'C:\Users\pandrews\source\repos\testingreadalltext\testingreadalltext\bin\Debug\netcoreapp3.1\??C:\Users\pandrews\countme.docx'

Translated: The name of the file, the name of the directory or the syntax of the volume label aren't correct.翻译:文件名、目录名或卷标语法不正确。

I can't see any error in my code but hopefully someone else might be able to.我在我的代码中看不到任何错误,但希望其他人能够看到。

EDIT: I want to add I am signed in as an admin, the user admin is the owner of the file and i've executed Visual Studio as administrator.编辑:我想添加我以管理员身份登录,用户管理员是文件的所有者,我以管理员身份执行了 Visual Studio。

Hans is correct in his comment.汉斯的评论是正确的。

The character is visible in your post when you inspect (with the browser tools) the HTML;当您检查(使用浏览器工具)HTML 时,该字符在您的帖子中可见; there you see the special character:"202A"在那里你会看到特殊字符:“202A” 在此处输入图片说明

Also, you might want to use the special environment folder constants instead of hard coding your path to your users folder: Eg此外,您可能希望使用特殊的环境文件夹常量,而不是硬编码您的用户文件夹路径:例如

var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "countme.docx");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM