0

I am trying to create a folder with a variable that can change, I am using

System.IO.Directory.CreateDirectory

When I hardcode a value like so:

var folder = @"C:\Users\Laptop\Documents\bot\random string here"

It creates that directory, but when I pass data to my method and try and use it like so:

var folder = @"C:\Users\Laptop\Documents\bot\" + 
 articlename.Replace(" ", "_");
        System.IO.Directory.CreateDirectory(folder);

It doesn't create it nor break. How can I use the variable to create a folder like that?

My article is a random string like "hello this is a article yadda"

  • More or less that should work. Sure you aren't seeing any exceptions? – BradleyDotNET Apr 14 '17 at 00:59
  • Does your random string have any invalid file name characters in it? – Rufus L Apr 14 '17 at 01:02
  • Turns out I had illegal characters, I thought removing spaces may have stopped that from having illgel charatcers fixed now :) – Richy Burnish Apr 14 '17 at 01:05
  • 1
    But spaces are valid characters. You can see the reserved characters [here](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#naming_conventions) – ProgrammingLlama Apr 14 '17 at 01:09
  • 1
    Spaces are valid path characters. You should use `Path.GetInvalidFileNameChars()` instead. See example here: http://stackoverflow.com/a/23182807/332528 – torvin Apr 14 '17 at 01:50
  • Your example with spaces was the one that worked, so spaces weren't the problem... :) – Rufus L Apr 14 '17 at 03:29

1 Answers1

-1

I solved by adding my paths together correctly as below

string path = root +"/"+ newfolder;
 if (!Directory.Exists(path))
 {
   Directory.CreateDirectory(path);
   // To move a file or folder to a new location:
   //System.IO.File.Move(fi.FullName, path);
 }
Spinstaz
  • 287
  • 6
  • 12