简体   繁体   中英

C# how do I draw on files in the same location as i'm running the program without a long path

So far I haven't found anything that would allow my program to access text files that are in the same folder as it. for example: if my file is in C:/testingfolder i would need to use C:/testingfolder/filenames.txt to access the other files, the problem with this is sometimes it wont be in c:/testingfolder but instead it might be in E:/importantfiles or F:/backup and it needs to run from all of those.

If anyone could explain or give code that showed how to make a longer path into a "same folder" path that would answer my question.

使用Environment.CurrentDirectory您可以获取正在执行的进程的路径,然后应使用System.IO.Path.Combine()方法将该路径与文件名连接起来,然后您将获得以下位置的绝对位置:您的文件。

You need to use System.IO and System.Text

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

then

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM