如何在 C# 中使用变量名创建文件夹?

发布于 2024-09-18 22:05:22 字数 1017 浏览 1 评论 0原文

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

效果很好。我能够创建该文件夹。但下面的代码不起作用。

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.CreateSubdirectory(klasorYolu2);
   Console.WriteLine("İkinci klasör de oluşturuldu...");

  }

 }

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

我收到“未处理的异常:System.ArgumentException:路径中存在非法字符”。错误。我发现了一些关于“路径类”的东西,但我无法得到明确的答案。

我应该怎么办?

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.CreateSubdirectory(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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

惯饮孤独 2024-09-25 22:05:22
 string klasorYolu="@\"H:\\"+a+"\"";

不要使字符串内容看起来像在 C# 程序中编写的内容。这应该看起来更像是:

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

一定要使用 Path.Combine() 方法,它负责将反斜杠放在正确的位置。

 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.

游魂 2024-09-25 22:05:22

尝试一下,

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

当您已经在字符串文字中时,无需添加这些 @"

Try

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文