开发者

How to create a folder using a variable name in C#?

开发者 https://www.devze.com 2023-01-15 13:28 出处:网络
Directory.CreateDirectory(@\"C:\\test\"); Works great. I\'m able to create the folder. BUT code below doesn\'t work.

Directory.CreateDirectory(@"C:\test");

Works great. I'm able to create the folder. BUT code below doesn't work.

using System;
using System.IO;
class iolar
{

 public static void klasorOlustur()
 {
  Console.WriteLine("Oluşturmak istediğiniz BİRİNCİ  klasörün adı?");
  string a=Console.ReadLine();
  Console.WriteLine("oluşturmak istediğiniz İKİNCİ klasörün adı?");
  string b=Console.ReadLine();
  Console.WriteLine("Klasörler oluşturuluyor.. Lütfen bekleyin...");

  string klasorYolu="@\"H:\\"+a+"\"";
  string klasorYolu2="\""+b+"\"";

  DirectoryInfo klasorcuk=new DirectoryInfo(klasorYolu);
  Console.Write(klasorYolu);
  if(klasorcuk.Exists==false)
  {
   klasorcuk.Create();
   Console.WriteLine("İlk klasör oluşturuldu...");
   DirectoryInfo klasorcuk2=klasorcuk.Cre开发者_如何学CateSubdirectory(klasorYolu2);
   Console.WriteLine("İkinci klasör de oluşturuldu...");

  }

 }

 static void Main()
 {
  klasorOlustur();
 }
}

I get "Unhandled Exception: System.ArgumentException: Illegal characters in path." error. I've found some stuff about "path class" but I couldn't get a clear answer.

What should I do?


 string klasorYolu="@\"H:\\"+a+"\"";

Don't make the string content look like what you write in a C# program. This ought to look more like:

 string klasorYolu = @"H:\" + a;

Be sure to use the Path.Combine() method, it takes care of putting the backslashes in the right place.


Try

 string klasorYolu = "H:\\" + a;
 string klasorYolu2 = b;

There is no need to add those @ and " when you're already inside a string literal.

0

精彩评论

暂无评论...
验证码 换一张
取 消