0

I am trying to create a file on the mapped drive but it is giving me an error :

Could not find a part of the path 'Y:\\score\\' where 'Y' is the mapped drive. There is a folder score in y drive and it has read and write permissions too.

Here is my code:

string filePath = "Y:\\score\\";
string[] lines = { "First line", "Second line", "Third line" };
using (StreamWriter outputFile = new StreamWriter(@filePath))
      {
         foreach (string line in lines)
             outputFile.WriteLine(line);
      }

Error comes when I am passing the filepath in the streamwriter. I am able to do this on my local drive but not on the mapped drive. I am not getting why this error is coming up. Please help me out.

Thanks

Vishal
  • 604
  • 1
  • 12
  • 25

2 Answers2

0

Your path should be your file path if you want read file you can use this

this code read all text to string File.ReadAllText(filePath);

this code read all line to the string array File.ReadAllLines(filePath);

0

You have no filename. So the StreamWriter does not know where to write the lines. You can fix it with

string filePath = "Y:\\score\\yourfilename.ext";
string[] lines = { "First line", "Second line", "Third line" };
using (StreamWriter outputFile = new StreamWriter(filePath))
{
    foreach (string line in lines)
        outputFile.WriteLine(line);
}

or this

string filePath = "Y:\\score\\";
string[] lines = { "First line", "Second line", "Third line" };
using (StreamWriter outputFile = new StreamWriter(filePath + "yourfilename.ext"))
{
    foreach (string line in lines)
        outputFile.WriteLine(line);
}

And you have to close the StreamWriter after writing.

Wudge
  • 357
  • 1
  • 6
  • 14