Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 读取文件失败,路径中存在非法字符?_C# - Fatal编程技术网

C# 读取文件失败,路径中存在非法字符?

C# 读取文件失败,路径中存在非法字符?,c#,C#,我正在尝试使用file.ReadAllLines()从资源文本文件(Word)中读取。但每次它运行时,我都会在路径中收到错误消息“非法字符”。有什么想法吗 //Get all of the words from the text file in the resources folder string[] AllWords = File.ReadAllLines(Conundrum.Properties.Resources.Words); 如果不知道您要传递的路径的值,就很难诊断此问题。但是,请

我正在尝试使用
file.ReadAllLines()
从资源文本文件(Word)中读取。但每次它运行时,我都会在路径中收到错误消息“非法字符”。有什么想法吗

//Get all of the words from the text file in the resources folder
string[] AllWords = File.ReadAllLines(Conundrum.Properties.Resources.Words);

如果不知道您要传递的路径的值,就很难诊断此问题。但是,请查看C函数
Path.GetInvalidFileNameChars()
Path.GetInvalidPathChars()
。有了这些,就可以很容易地找出你的路径有什么问题

var path = Conundrum.Properties.Resources.Words;
var invalidPathChars = path.Where(Path.GetInvalidPathChars().Contains).ToArray();
if (invalidPathChars.Length > 0)
{
    Console.WriteLine("invalid path chars: " + string.Join(string.Empty, invalidPathChars));
}

var fileName = Path.GetFileName(path);
var invalidFileNameChars = fileName .Where(Path.GetInvalidFileNameChars().Contains).ToArray();
if (invalidFileNameChars .Length > 0)
{
    Console.WriteLine("invalid file name chars: " + string.Join(string.Empty, invalidFileNameChars ));
}

至于资源文件,并不是说这些文件通常编译到程序集中,因此可能无法在磁盘上以原始形式提供。您可以使用内置属性或Assembly.GetManifestResourceStream()检索它们。

如果它实际上作为资源添加到项目中,请尝试直接引用它:

var allWords = Conundrum.Properties.Resources.Words;
如果要将其放入数组中,请在换行符上拆分:

var allWords = Conundrum.Properties.Resources.Words
                 .Split(new[] { Environment.NewLine }, StringSplitOptions.None);

我只是试了一下,这对我很有效。使用
File.ReadAllLines
引发了相同的异常。。。我假设它实际上需要一个指向磁盘上文件的完整文件路径。

我已经尝试了一些方法,这似乎是最好的

string resource_data = Properties.Resources.Words;
string[] AllWords = resource_data.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

我知道这是一个古老的问题,但当我在这个主题的这些部分徘徊时。。。以下是我的看法:

据我所见,资源文本文件被视为一个文本文件,希望
file.ReadAllLines(Conundrum.Properties.Resources.Words)
将.txt文件中的行作为字符串读取

但是,通过使用
ReadAllLines()
这种方式,它会立即读取第一行作为文件路径名,因此会显示错误消息

我建议根据添加文件作为资源

然后,根据,稍加修改,您将能够读取文本文件内容:

        using System.Reflection; //Needs this namespace for StreamReader
        
        var assembly = Assembly.GetExecutingAssembly();
        
        //Ensure you include additional folder information before filename
        var resourceName = "Namespace.FolderName.txtfile.txt"; 

        using (Stream stream = assembly.GetManifestResourceStream(resourceName))
        using (StreamReader sr = new StreamReader(stream))
        {
            string line;

            while ((line = sr.ReadLine()) != null)
            {
                //As an example, I'm using a CSV textfile
                //So want to split my strings using commas
                string[] fields = line.Split(',');                  
                
                string item = fields[0];
                string item1 = fields[1];
            }
            sr.Close();
        }

这是我添加的.txt文件的资源文件。文件目录的全名是什么?原始目录是我的桌面,但我试图从资源文件中访问它。这个问题以前已经被问过了。