Reputation: 23
I have created a code to create folders with two Textboxes.
I would like to be able to check if the customer number exists before creating the folder. The newly created folder will be the combination of the two Textboxes (this is already solved). I just need to be able to determine if the folder exists only with the customer number, as it is probably created with (customer number + customer name).
Current working code:
{
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 exists\nDo 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 exist\nWant 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);
}
}
Upvotes: 2
Views: 535
Reputation: 2362
string[] dirs = Directory.GetDirectories(@"c:\", txtTextBox.Text + "*");
this will only get directrories starting with the desired Text
Edit: This is only a good solution if the customer number has fixed places (in you exaple 4 from 0000-9999)
Microsoft Documentation - check example below
Upvotes: 0
Reputation: 1024
You could also each time you need the folder just do that:
public static void Main()
{
var username = "someuser";
var usernumber = "ABC123";
var mainDirectory = @"C:\Path\To\The\Main\Dir";
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);
}
Upvotes: 1