using System;
using System.IO;
namespace GetFilesFromDirectory
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Write your Name of Disc");
string myDisc = Console.ReadLine();
string myDisc1 = "@\"";
Console.WriteLine("Write your Directory");
string myDir1 = Console.ReadLine();
string myDir = ":\\";
string myDir2 = "\\\"";
string myPath = myDisc1 + myDisc + myDir + myDir1 + myDir2;
Console.WriteLine(myPath);
string[] filePaths = Directory.GetFiles(myPath);
foreach (var files in filePaths)
{
Console.WriteLine(files);
}
Console.ReadLine();
}
}
}

- 26,353
- 15
- 70
- 71

- 47
- 2
- 6
-
You are probably making the wrong path, debug and see what you have in myPath before you call GetFiles. This myPath values should open the given folder – Adil Apr 16 '15 at 10:06
-
5What exactly is your question? Do you get compiler errors? Logical errors? What did you expect to happen, what *does* happen? – nvoigt Apr 16 '15 at 10:06
-
Show at least an example of contents of `myPath` – DrKoch Apr 16 '15 at 10:11
-
2Use `Path.Combine()` in your code. They invented it for these cases. – DrKoch Apr 16 '15 at 10:11
-
The compiler fails to compile. It does not like some signs that I use to complete the path in myPath variable. – CsharpLover Apr 16 '15 at 10:13
2 Answers
From what I can tell your myPath
will look like @"discName:\dirName\"
, you don't need to append the @"
or "
.
These symbols are used when you are creating a new string variable to note that is a String literal, but you are including these characters in the actual string you are generating.
In other words, remove myDisc1
and myDir2
Better than that, as noted by DrKoch
string myPath = Path.Combine(myDisc + @":\", myDir1);

- 42,633
- 14
- 77
- 146
-
-
1The slash won't be added if the path is a drive reference http://stackoverflow.com/questions/19909008/path-combine-does-not-add-directory-separator-after-drive-letter – alessio bortolato Apr 16 '15 at 10:55
Try this
static void Main(string[] args)
{
Console.WriteLine("Write your Name of Disc");
//You need to add :\ to make it a fullPath
string myDisc = Console.ReadLine()+":\\";
Console.WriteLine("Write your Directory");
string myDir1 = Console.ReadLine();
string myPath = Path.Combine(myDisc , myDir1);
Console.WriteLine(myPath);
string[] filePaths = Directory.GetFiles(myPath);
foreach (var files in filePaths)
{
Console.WriteLine(files);
}
Console.ReadLine();
}
What are you doing is creating a string wich is the literal reprentation of the string you want but you don't need to do this.
For example if you write this:
string path=@"c:\dir\subdir";
its real value will be c:\dir\subdir
instead this "@\"c:\\dir\\subdir\""
; will be
@"c:\dir\subdir"
Read these articles to better understand string literals and verbatim strings https://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx
https://msdn.microsoft.com/en-us/library/362314fe.aspx
https://msdn.microsoft.com/en-us/library/h21280bw.aspx

- 241
- 2
- 9