How to read existing text files without defining path

后端 未结 8 1039
太阳男子
太阳男子 2020-12-14 05:53

Most of the examples shows how to read text file from exact location (f.e. \"C:\\Users\\Owner\\Documents\\test1.txt\"). But, how to read text files without writing full path

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-14 06:17

    As your project is a console project you can pass the path to the text files that you want to read via the string[] args

    static void Main(string[] args)
    {
    }
    

    Within Main you can check if arguments are passed

    if (args.Length == 0){ System.Console.WriteLine("Please enter a parameter");}
    

    Extract an argument

    string fileToRead = args[0];
    

    Nearly all languages support the concept of argument passing and follow similar patterns to C#.

    For more C# specific see http://msdn.microsoft.com/en-us/library/vstudio/cb20e19t.aspx

提交回复
热议问题