Reputation:
private static void saveDirAndFiles(TreeNode currNode)
{
using (StreamWriter file = new StreamWriter("test.html"))
{
if(currNode.ValueType=="DIR") //Are you a File or Directory
{
file.WriteLine(currNode.Value);//relative Name of Directory
}
else
{
string[] file1 = File.ReadAllLines(Path.GetFullPath(currNode.Value)); //EXEPTION
string prgcode = "";
foreach (string line in file1)
{
prgcode += line;
}
file.WriteLine(currNode.Value);//relative Name of File +...
file.WriteLine(String.Format("<code><pre>{0}</pre></code>", prgcode)); //... The Content of the File
}
}
foreach (TreeNode item in currNode.ChildNodes)
{
saveDirAndFiles(item);
}
}
The function searches the absolute Path only in the project I'm currently working on. My Project is on the Desktop the file Name is on the C Directory. The Exception says : Project\bin\debug\Filename not found.
Upvotes: 0
Views: 102
Reputation: 5781
Path.GetFullPath
gives you the absolute file path for a given path sequence that may be either absolute or relative. It does not even check whether the file exists, nor does it perform a search in the file system.
If you want to load your files from somewhere else, you thus have two options: Either you rebase the relative path explicitly to a different base path using Path.Combine
(preferable option) or you change the environments current working directory (may have side-effects in other parts of your application).
Upvotes: 1