Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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# Streamread数组C_C#_C# 4.0_Stream_Openfiledialog - Fatal编程技术网

C# Streamread数组C

C# Streamread数组C,c#,c#-4.0,stream,openfiledialog,C#,C# 4.0,Stream,Openfiledialog,string[]filenames=openFileDialog1.filenames 如何读取此数组 我是否需要将每个文件路径设置为自己的字符串??? 我不知道。你的意思是访问数组中的元素吗 for (int i = 0; i < filenames.Length; i++) MyDoSomethingMethod(filenames[i]); 当您访问文件名[some_index]时,您已经访问了字符串,因此无需将其保存到另一个变量来使用它,这同样适用于foreach循环。假

string[]filenames=openFileDialog1.filenames

如何读取此数组

我是否需要将每个文件路径设置为自己的字符串???
我不知道。

你的意思是访问数组中的元素吗

for (int i = 0; i < filenames.Length; i++)
    MyDoSomethingMethod(filenames[i]);

当您访问文件名[some_index]时,您已经访问了字符串,因此无需将其保存到另一个变量来使用它,这同样适用于foreach循环。

假设您希望循环访问文件名并对每个文件执行某些操作,下面是一个示例:

foreach (String file in openFileDialog1.FileNames) 
{
    // read file lines
    try
    {

        using (StreamReader sr = File.OpenText(file))
        {
            String input;
            while ((input = sr.ReadLine()) != null)
            {
                Console.WriteLine(input);
            }
            Console.WriteLine ("The end of the stream has been reached.");
        }


    }
    catch (SecurityException ex)
    {
        // The user lacks appropriate permissions to read files, discover paths, etc.
        Console.WriteLine ("Security error. Please contact your administrator for details.\n\n" +
            "Error message: " + ex.Message + "\n\n" +
            "Details (send to Support):\n\n" + ex.StackTrace
        );
    }
    catch (Exception ex)
    {
        // Could not load the file - probably related to Windows file system permissions.
        Console.WriteLine ("Cannot display the file: " + file.Substring(file.LastIndexOf('\\'))
            + ". You may not have permission to read the file, or " +
            "it may be corrupt.\n\nReported error: " + ex.Message);
    }
}

你的意思不清楚,请提供更多信息。你能澄清一下吗。。。您到底想做什么?因此,提供了有关FileNames属性的信息。您是想打开它们并阅读它们,还是只是反复浏览它们?它们已经是数组中的字符串,所以如果您愿意,可以直接通过它们进行foreach。您需要提供更多信息,以便我们知道您正在尝试做什么。
foreach (String file in openFileDialog1.FileNames) 
{
    // read file lines
    try
    {

        using (StreamReader sr = File.OpenText(file))
        {
            String input;
            while ((input = sr.ReadLine()) != null)
            {
                Console.WriteLine(input);
            }
            Console.WriteLine ("The end of the stream has been reached.");
        }


    }
    catch (SecurityException ex)
    {
        // The user lacks appropriate permissions to read files, discover paths, etc.
        Console.WriteLine ("Security error. Please contact your administrator for details.\n\n" +
            "Error message: " + ex.Message + "\n\n" +
            "Details (send to Support):\n\n" + ex.StackTrace
        );
    }
    catch (Exception ex)
    {
        // Could not load the file - probably related to Windows file system permissions.
        Console.WriteLine ("Cannot display the file: " + file.Substring(file.LastIndexOf('\\'))
            + ". You may not have permission to read the file, or " +
            "it may be corrupt.\n\nReported error: " + ex.Message);
    }
}