✅ I cant combine folder paths

I was trying to combine folder path with strings with that code
c#
string ilce = duzenleIlce.Text;
string mahalle = duzenleMah.Text;
string ada = duzenleAda.Text;
string parsel = duzenleParsel.Text;
string islem = duzenleIslemCombo.Text;
string klasorAdi = string.IsNullOrEmpty(ada) ? parsel : ada + "_" + parsel;
string baseFolderPath = Settings1.Default.settingsFolderPath + "\\LIHKAB_3100-800\\";

string folderPath = Path.Combine(baseFolderPath, ilce, mahalle, klasorAdi);
c#
string ilce = duzenleIlce.Text;
string mahalle = duzenleMah.Text;
string ada = duzenleAda.Text;
string parsel = duzenleParsel.Text;
string islem = duzenleIslemCombo.Text;
string klasorAdi = string.IsNullOrEmpty(ada) ? parsel : ada + "_" + parsel;
string baseFolderPath = Settings1.Default.settingsFolderPath + "\\LIHKAB_3100-800\\";

string folderPath = Path.Combine(baseFolderPath, ilce, mahalle, klasorAdi);
But when i click the button, baseFolderPath is still use same path, i need to combine "ilce, mahalle,klasorAdi" strings to reach my folder path. How can i make?
5 Replies
ThєSαvícs ↝ darKSaviC
it use like that without adding any path "C:\Users\ASUS\Documents\LIHKAB\LIHKAB_3100-800" i need paths like that "C:\Users\ASUS\Documents\LIHKAB\LIHKAB_3100-800\ISLEM\ILCE\MAHALLE\klasorAdi" and i need 1 more thing "C:\Users\ASUS\Documents\LIHKAB\LIHKAB_3100-800\ISLEM\ILCE\MAHALLE\klasorAdi\Note.txt"
SpReeD
SpReeD8mo ago
So, it's actually working and your code is missing the filename Note.txt, did I get that right?
ThєSαvícs ↝ darKSaviC
My code is working, but only the folder path in "baseFolderPath" is visible. In my code, there is a place where I display the content of "folderPath" with MessageBox, and there the code tells me this: Program location: C:\Users\ASUS\Documents\LIHKAB\LIHKAB_3102-811\ The program only shows me this location. I need a way for the other strings (ilce, mahalle, klasorAdi) that I added to "folderPath" to be included as well. Because in the continuation of the code;
string targetFolderPath = Path.Combine(folderPath);
string targetNotePath = Path.Combine(targetFolderPath, "Not.txt");

try
{
string folderPathN = Path.Combine(targetNotePath);
string folderPathF = Path.Combine(targetFolderPath);

// Delete Note
if (File.Exists(folderPathN))
{
File.Delete(folderPathN);
}

// Check folder exists
if (Directory.Exists(folderPathF))
{
// Delete Folder
Directory.Delete(folderPathF, false);
MessageBox.Show("Program location: " + folderPathF, "Location Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show("Folder successfully deleted.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Program location: " + folderPathF, "Location Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show("The specified subfolder could not be found.", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}
}
catch (Exception ex)
{
string folderPathF = Path.Combine(targetFolderPath);

MessageBox.Show("Program location: " + folderPathF, "Location Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show("An error occurred while deleting the folder: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
string targetFolderPath = Path.Combine(folderPath);
string targetNotePath = Path.Combine(targetFolderPath, "Not.txt");

try
{
string folderPathN = Path.Combine(targetNotePath);
string folderPathF = Path.Combine(targetFolderPath);

// Delete Note
if (File.Exists(folderPathN))
{
File.Delete(folderPathN);
}

// Check folder exists
if (Directory.Exists(folderPathF))
{
// Delete Folder
Directory.Delete(folderPathF, false);
MessageBox.Show("Program location: " + folderPathF, "Location Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show("Folder successfully deleted.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Program location: " + folderPathF, "Location Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show("The specified subfolder could not be found.", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}
}
catch (Exception ex)
{
string folderPathF = Path.Combine(targetFolderPath);

MessageBox.Show("Program location: " + folderPathF, "Location Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show("An error occurred while deleting the folder: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
I want to delete both the folder and the "Not.txt" files inside the folder. However, I cannot update the file location with this code. The result is always the same: Program location: C:\Users\ASUS\Documents\LIHKAB\LIHKAB_3102-811\ i tried to edit string when i put actual text string ilce = "arsuz" and changed messagebox's code ("Program location: " + ilce... its works but when i put duzenleIlce.Text its not working i dont know why sorry i forgot to reply to you
SpReeD
SpReeD8mo ago
Np, however it's a bit of spaghetti code here, it's unnecessary to reassign targetNotePath to yet another Path.Combine built string. Try using string interpolation when concatenating strings, it helps a lot with readability, plus you don't need to call ToString everytime. Example:
string a = "foo";
string b = "bar";
string myString = $"this is just a {a} string, showing string interpolation {a}{b}";
string a = "foo";
string b = "bar";
string myString = $"this is just a {a} string, showing string interpolation {a}{b}";
myString = this is just a foo string, showing string interpolation foobar The Directory class has a GetFiles method which also implements a simple search pattern as second parameter, besides that, you can recursively delete a folder by setting the recursive option to true; second parameter of Directory.Delete method. So, what is the exception? in ex.Message
ThєSαvícs ↝ darKSaviC
its just gives me which thing is missing or wrong and i took only that messagebox codes from chatgpt i fully redesigned code, now code getting datas from datagridview its done
Want results from more Discord servers?
Add your server