-1

我想通过打开具有特定扩展名的文件(这里没问题)直接启动我的软件,但我想知道如何直接读取该文件的内容?

我在网上查了一下,但没有发现任何有用的东西。

谢谢

4

2 回答 2

1

尝试使用File该类,尤其是File.ReadAllText Method

示例 MSDN 用法

using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        // This text is added only once to the file. 
        if (!File.Exists(path))
        {
            // Create a file to write to. 
            string createText = "Hello and Welcome" + Environment.NewLine;
            File.WriteAllText(path, createText);
        }

        // This text is always added, making the file longer over time 
        // if it is not deleted. 
        string appendText = "This is extra text" + Environment.NewLine;
        File.AppendAllText(path, appendText);

        // Open the file to read from. 
        string readText = File.ReadAllText(path);
        Console.WriteLine(readText);
    }
}

为了从命令行读取文件名(双击调用)

using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main(string[] args)
    {
         Console.Writeline("I bet your filename is: {0}", args[0]);
    }
}

另请参阅这个非常好的示例

于 2013-06-11T08:21:37.453 回答
0

File.ReadAllLines是你要找的,你应该提供一个 Serilizer 来从那个文件中获取你的特定数据。

但是你想从那个文件中读取什么?如果它是可执行文件,我认为您不想读取该文件还是?

于 2013-06-11T08:19:44.733 回答