[英]C# how do I draw on files in the same location as i'm running the program without a long path
到目前为止,我还没有发现任何可以让我的程序访问与其所在文件夹中的文本文件的内容。 例如:如果我的文件在C:/ testingfolder中,我将需要使用C:/testingfolder/filenames.txt来访问其他文件,但与此相关的问题是有时它不会在c:/ testingfolder中,但可能是在E:/ importantfiles或F:/ backup中,它需要从所有这些文件中运行。
如果有人可以解释或提供代码,说明如何将更长的路径转换成可以回答我的问题的“相同文件夹”路径。
使用Environment.CurrentDirectory
您可以获取正在执行的进程的路径,然后应使用System.IO.Path.Combine()
方法将该路径与文件名连接起来,然后您将获得以下位置的绝对位置:您的文件。
您需要使用System.IO
和System.Text
using System;
using System.IO;
using System.Text;
然后
static void Main(string[] args)
{
string line = "";
// look for the file "myfile.txt" in application root directory
using (StreamReader sr = new StreamReader("myfile.txt"))
{
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
Console.ReadKey();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.