0

I am trying to make to create a .cmd file with this code into it: call .\CopyToTarget.cmd w60 glb "C:\Users\oma\me\trunk-r664\USB-map". I am creating this code ~5 times.

But since \trunk-r664\ is already in use it seems like I cannot write: @"\trunk-r664\USB-map" into the .cmd file for some reason. Does anyone know how to fix it? It keeps getting me the error: UnauthorizedAccesExpection was unhandled, ccess to the path 'C:\Users\me\Desktop\trunk-r664\USB-map' is denied.

using (StreamWriter sw = File.CreateText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) 
+ "\\trunk-r664\\trunk\\cmd\\custom\\RunAll.cmd"))
{
    for (int j=0;j<installeerlijst64.Count;j++)
    {
        sw.WriteLine("call .\\CopyToTarget.cmd " + installeerlijst64[j] + " glb" + 
File.CreateText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\trunk-r664\USB-map"));
    }
}

I tried this too, but it tells me I am using an illegal character:

"\""+File.CreateText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
+ @"\trunk-r664\USB-map" + "\""));
wouter
  • 359
  • 4
  • 15
  • Please note that `UnauthorizedAccessException` and `already in use` are two different errors and may occurred due to two different reasons. – Ricky Jul 06 '15 at 09:18

1 Answers1

1

File.CreateText will create a new file. First time when for loop execute, it will create and open the file USB-map and hold the handle of that file. During second iteration of for loop, it will try to do the same thing. Hence, already in use error.

Remove File.CreateText and you will get the desired result.

sw.WriteLine("call .\\CopyToTarget.cmd " + installeerlijst64[j] + " glb " + "\"" + 
Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\trunk-r664\USB-map" + "\"");
Ricky
  • 2,323
  • 6
  • 22
  • 22