Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#读取我的项目之外的文件';s目录_C# - Fatal编程技术网

C#读取我的项目之外的文件';s目录

C#读取我的项目之外的文件';s目录,c#,C#,我是C#新手,我试图从我的下载目录中读取一个文本文件,因此我需要为它提供绝对路径,但C#一直在这个目录中查找:project/bin/Debug,以查找该文件。 我的操作系统是ubuntu,我的文件位于/home/my name/download/file.txt 这就是C#想要的:/project/bin/Debug/home/my name/download/file.txt 有没有办法告诉C#这是一条绝对路径 FileName = Path.Combine("/home/my-n

我是C#新手,我试图从我的下载目录中读取一个文本文件,因此我需要为它提供绝对路径,但C#一直在这个目录中查找:project/bin/Debug,以查找该文件。 我的操作系统是ubuntu,我的文件位于/home/my name/download/file.txt 这就是C#想要的:/project/bin/Debug/home/my name/download/file.txt 有没有办法告诉C#这是一条绝对路径

FileName = Path.Combine("/home/my-name/download/file.txt".Split(Path.DirectorySeparatorChar));
string[] lines = File.ReadAllLines($@"{FileName}");
        
foreach (string line in lines)
{
   Console.WriteLine("\t" + line);
}

只是澄清一下;文件名不是我的问题。当我将它传递给这个方法=>File.ReadAllLines()时,它会在文件名的开头添加“project/bin/Debug”。所以它寻找这条路径⇒ /project/bin/Debug/home/my name/download/file.txt它不存在

string usrPath=Path.Combine(Environment.GetFolderPath(SpecialFolder.UserProfile,SpecialFolderOption.DoNotVerify),“您的名字”);//请是的,有一种方法,但是没有代码就很难给出指导。这里的Split()只是让人头痛不已,想知道为什么要使用它。我添加了一段代码。要我也共享错误吗?删除路径并拆分。“你已经有了完整的路径。@汉帕桑你是对的,我尝试了100种方法让它工作!这就是为什么它现在没有意义!我会设法解决它的!
string usrPath = Path.Combine(Environment.GetFolderPath(SpecialFolder.UserProfile, SpecialFolderOption.DoNotVerify), "YOUR NAME"); // <= home/your-name/ (also u can use Environment.UserName to get your username)
string dwnlPath = Path.Combine(usrPath, "Downloads"); // "Downloads" folder
string filePath = Path.Combine(dwnlPath, "file.txt"); // file.txt path