我已经创建了一个代码来创建带有两个文本框的文件夹。
- Textbox1 -客户号码(XXXX).
- Textbox2 -客户名称。
我希望能够在创建文件夹之前检查客户号码是否存在。新创建的文件夹将是两个文本框的组合(这个问题已经解决了)。我只需要能够确定文件夹是否只存在客户编号,因为它可能是用(客户编号+客户名称)创建的。
当前工作代码:
{
string no = textBox1.Text;
string client = textBox2.Text;
string carpeta = @"C:" + no + " " + client;
string sourcePath = @"C:main";
string destinationPath = @"C:" + no + " " + client;
textBox1.Clear();
textBox2.Clear();
try
{
if (Directory.Exists(carpeta))
{
DialogResult y;
y = MessageBox.Show("Folder already existsnDo you want to open it?", "AE.", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (y == DialogResult.Yes)
{
System.Diagnostics.Process.Start(@"C:" + no + " " + client);
}
else
{
Close();
}
}
else
{
DialogResult x;
x = MessageBox.Show("The folder doesn't existnWant to create a folder?." + "n" + no + " " + client, "AE.", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (x == DialogResult.Yes)
{
Directory.CreateDirectory(carpeta);
FileSystem.CopyDirectory(sourcePath, destinationPath, UIOption.AllDialogs);
System.Diagnostics.Process.Start(@"C:" + no + " " + client);
}
else
{
Close();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error:" + ex.Message);
}
}
你也可以在每次需要文件夹的时候这样做:
public static void Main()
{
var username = "someuser";
var usernumber = "ABC123";
var mainDirectory = @"C:PathToTheMainDir";
var pathToTheUserDirectory = Path.Combine(mainDirectory, $"{username}-{usernumber}");
// This line will create the directory if not exist or take the existing directory.
var directoryInfo = Directory.CreateDirectory(pathToTheUserDirectory);
var directoryPath = directoryInfo.FullName;
// ...
// or
// directoryInfo.Delete(recursive: true);
}
string[] dirs = Directory.GetDirectories(@"c:", txtTextBox.Text + "*");
这将只获得以所需Text
开头的目录编辑:只有当客户号码有固定的位置时,这才是一个好的解决方案(在您的示例4中,从0000到9999)
Microsoft Documentation -查看下面的示例