I'm currently storing a file path for a file I would like to open and read in for my program. However when I put that path into File.OpenText it adds the path to the current directory to the file path and then I get this error:
DirectoryNotFoundException: Could not find a part of the path "/Users/km/Desktop/MP/file:/Users/km/Downloads/PT07E.obj".
The path I want is the one I am passing in, which is file:/Users/km/Downloads/PT07E.obj".
Is there a way to stop File.OpenText from adding to this path I am passing in originally?
You have to add a correct absolute path - that means, you forgot to define the drive (normally it's C ). So your path would have to be (I'd use backslashes):
@"C:\Users\km\Downloads\PT07E.obj"
However it's a better idea not to use an absolute path for this. I'd use the specialFolder option of c#:
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downlads", "PT07E.obj");
And it's always a good idea to use Path.Combine
, that uses the standard path splitter of the OS.
If you are operating with paths like file:/{absolute path}
(which is actually an URI
) you may use System.Uri
class.
As in this answer .
var uri = new Uri("file:/Users/km/Downloads/PT07E.obj");
using (var reader = File.OpenText(uri.AbsolutePath))
{
...
}
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.