从未知位置读取文件? [英] Read a file from an unknown location?
问题描述
我从微软那里得到了这个读取文件代码
I have got this read file code from microsoft
@"C:\Users\computing\Documents\mikec\assignment2\task_2.txt"
当我在做这件事时效果很好,但是当我要交这个作业时,我的讲师不会和我有相同的目录.
That works fine when im working on it, but when i am to hand in this assignment my lecturer isn't going to have the same directory as me.
所以我想知道是否有办法从保存程序的文件中读取它?
So i was wondering if there is a way to read it from just the file the program is held in?.
我想我可以将它添加为资源,但我不确定这是否是允许在任何文件中允许的分配的正确方式.
I was thinking i could add it as a resource but im not sure if that is the correct way for the assignment it is meant to allow in any file.
谢谢
推荐答案
您可以跳过路径 - 这将从程序的工作目录中读取文件.
You can skip the path - this will read file from the working directory of the program.
只要@"task_2.txt"
就可以了.
UPDATE:请注意该方法在某些情况下不起作用.如果您的讲师使用一些自动运行程序(脚本、应用程序)来验证您的应用程序,那么@ken2k 的解决方案将更加强大.
UPDATE: Please note that method won't work in some circumstances. If your lecturer uses some automated runner (script, application whatsoever) to verify your app then @ken2k's solution will be much more robust.
这篇关于从未知位置读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!