如何在 C# 中使用变量名创建文件夹?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使字符串内容看起来像在 C# 程序中编写的内容。这应该看起来更像是:
一定要使用 Path.Combine() 方法,它负责将反斜杠放在正确的位置。
Don't make the string content look like what you write in a C# program. This ought to look more like:
Be sure to use the Path.Combine() method, it takes care of putting the backslashes in the right place.
尝试一下,
当您已经在字符串文字中时,无需添加这些
@
和"
。Try
There is no need to add those
@
and"
when you're already inside a string literal.