I am using two ShowDialog windows. I want to save the second file in the folder where I saved the first file. Before saving a file, I want to press the OK button without displaying the second ShowDialog window. Or I want to save it to the same location without viewing it at all. How will we achieve this?
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "Excele Aktar";
sfd.Filter = "Excel (xlsx)|*.xlsx";
sfd.FileName = "Ögrencibil";
if (sfd.ShowDialog() == DialogResult.OK)
{
Export_DataGridView_To_Excel(DGV, sfd.FileName);
}
SaveFileDialog sfd2 = new SaveFileDialog();
sfd2.Title = "Worde Aktar";
sfd2.Filter = "Word (doc)|*.docx";
sfd2.FileName = "Ögrencibil";
if (sfd2.ShowDialog() == DialogResult.OK)
{
string wordosyam = Path.GetFullPath(sfd2.FileName);
}

Mayooran NavamanyRank #438 · 5.7K reputationPosted Jan 27, 2024, 10:35 AM EST
If you want to save the second file in the same location as the first file without displaying the second
SaveFileDialog, you can achieve this by extracting the directory from the first file's path and then appending the desired file name for the second file.SaveFileDialog sfd = new SaveFileDialog(); sfd.Title = "Excel Aktar"; sfd.Filter = "Excel (xlsx)|*.xlsx"; sfd.FileName = "Ögrencibil"; if (sfd.ShowDialog() == DialogResult.OK) { Export_DataGridView_To_Excel(DGV, sfd.FileName); string directory = Path.GetDirectoryName(sfd.FileName); // Construct the path for the second file in the same directory string secondFilePath = Path.Combine(directory, "Ögrencibil_Word.docx"); SaveSecondFile(secondFilePath); } // Function to save the second file private void SaveSecondFile(string filePath) { // Your logic to save the second file goes here }This way, after saving the first file, the code constructs the path for the second file in the same directory without displaying the second
SaveFileDialog. You can adjust the file name and extension as needed.Mehmet FatihRank #1,474 · 1.1K reputationPosted Jan 27, 2024, 4:15 PM EST
Thank you very much. Mayooran Navamany. It works fine.