The following was given by my instructor and on my friends Win 7 it works just fine. I have tried my work laptop and my personal desktop…keeps finding the following error
I dont get it…I went as far as changing permissions to the directory to everyone just for kicks…it works on his copy paste but not mine.
“Could not find a part of the path ‘c:\Users\Wookie\My Documents\’.”
using System;
using System.IO;
class Program
{
static void Main()
{
// This line of code gets the path to the My Documents Folder
string environment = System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal) + "\\";
Console.WriteLine("Enter a file name in My Documents: ");
string input = Console.ReadLine();
// concatenate the path to the file name
string path = environment + input;
// now we can use the full path to get the document
StreamReader myFile = new StreamReader(path);
int n = int.Parse(myFile.ReadLine());
Console.WriteLine("read {0}", n);
Console.ReadLine();
}//End Main()
}//End class Program
Try whether the file actually exists from your program’s point of view. Of course, replace
yourfile.txtwith the filename of the file you are looking for.If it doesn’t, try the same in your file system explorer. Otherwise, are you sure you’ve entered the file name correctly? Try hard-coding it temporarily. The above code also shows how you should combine paths (note the lack of a slash (
\) as it is inserted automatically) and how to check whether a file (or directory, usingDirectory.Exists()) exists. This may be of use in finding where the problem is.