-1

I have folders named TST-D0123, TST-D0245, TST-D0568. This folders are being listed in the combobox. Now, I have to set the path to generate new DataFile.txt based on folders name. How can I do that?

This is the combobox code:

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    if ((string)comboBox2.SelectedItem == "TST-D0123")
    {
        Path = $@"G:\Eng\Share\PC\TST-D0123\SSD\SN\D0123_DataFile.txt";
        logPath = $@"G:\Eng\Share\PC\TST-D0123\SSD\SystemLog\SystemLog_{DateTime.Now:yyyyMMdd-HHmm}.txt";

        generateFile()
    }
    else if ((string)comboBox2.SelectedItem == "TST-D0245")
    {
        Path = $@"G:\Eng\Share\PC\TST-D0245\SSD\SN\D0245_DataFile.txt";
        logPath = $@"G:\Eng\Share\PC\TST-D0245\SSD\SystemLog\SystemLog_{DateTime.Now:yyyyMMdd-HHmm}.txt";

        generateFile()
    }
    else 
    {
        Path = $@"G:\Eng\Share\PC\TST-D0568\SSD\SN\D0568_DataFile.txt";
        logPath = $@"G:\Eng\Share\PC\TST-D0568\SSD\SystemLog\SystemLog_{DateTime.Now:yyyyMMdd-HHmm}.txt";

        generateFile();
    }
    
}

This is the code to generate new file DataFile.txt .

private void generateFile()
{
    dbConnect();
    SqlCommand command_Data;
    SqlDataReader dataReader;
    String sql = "";

    sql = "SELECT TOP(1) start_rg, end_rg FROM Rg WHERE status = 'Avail' ";
    command_Data = new SqlCommand(sql, cnn);

    dataReader = command_Data.ExecuteReader();


    //how to set this path based on selected text in combobox?
    string Path = $@"G:\Eng\Share\PC\**foldername**\SSD\SN\**4char from foldername**_DataFile.txt";


    using (StreamWriter tw = File.CreateText(Path))
    {
        while (dataRead.Read())
        {
            tw.WriteLine("RG");
            tw.WriteLine("StartRG={0}", dataRead["start_rg"]);
            tw.WriteLine("EndRG={0}", dataRead["end_rg"]);
        }
    }
    dataReader.Close();
    command_Data.Dispose();
    
}
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69

3 Answers3

0

To begin with, since the folder names are the items in ComboBox and hence the SelectedItem, you could directly use it to create the Path and logPath rather than a set of if conditions and explicitly assigning path.

For example,

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    if(comboBox2.SelectedItem is string folder && !string.IsNullOrEmpty(folder) && folder.Contains("-"))
    {
        Path = $@"G:\Eng\Share\PC\{folder}\SSD\SN\{folder.Split('-')[0]}_DataFile.txt";
        logPath = $@"G:\Eng\Share\PC\{folder}\SSD\SystemLog\SystemLog_{DateTime.Now:yyyyMMdd-HHmm}.txt";

        generateFile(Path); //explained below
    }
}

You could pass the Path variable directly to the GenerateFile method since it doesn't make sense to create the path twice.

private void generateFile(string path)
{
    dbConnect();
    SqlCommand command_Data;
    SqlDataReader dataReader;
    String sql = "";

    sql = "SELECT TOP(1) start_rg, end_rg FROM Rg WHERE status = 'Avail' ";
    command_Data = new SqlCommand(sql, cnn);

    dataReader = command_Data.ExecuteReader();
    
    using (StreamWriter tw = File.CreateText(path))
    {
        while (dataRead.Read())
        {
            tw.WriteLine("RG");
            tw.WriteLine("StartRG={0}", dataRead["start_rg"]);
            tw.WriteLine("EndRG={0}", dataRead["end_rg"]);
        }
    }
    dataReader.Close();
    command_Data.Dispose();
}
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
0

Just pass chosen file name to generateMethod . You would need to change the signature of the method to acept a string. Your code should look like this:

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    var folderName = comboBox2.SelectedItem.ToString();
    
    if (folderName == "TST-D0123")
    {
        Path = $@"G:\Eng\Share\PC\TST-D0123\SSD\SN\D0123_DataFile.txt";
        logPath = $@"G:\Eng\Share\PC\TST-D0123\SSD\SystemLog\SystemLog_{DateTime.Now:yyyyMMdd-HHmm}.txt";
    }
    else if (folderName == "TST-D0245")
    {
        Path = $@"G:\Eng\Share\PC\TST-D0245\SSD\SN\D0245_DataFile.txt";
        logPath = $@"G:\Eng\Share\PC\TST-D0245\SSD\SystemLog\SystemLog_{DateTime.Now:yyyyMMdd-HHmm}.txt";
    }
    else 
    {
        Path = $@"G:\Eng\Share\PC\TST-D0568\SSD\SN\D0568_DataFile.txt";
        logPath = $@"G:\Eng\Share\PC\TST-D0568\SSD\SystemLog\SystemLog_{DateTime.Now:yyyyMMdd-HHmm}.txt";
    }
    
    generateFile(folderName);
}

private void generateFile(string folderName)
{
    dbConnect();
    SqlCommand command_Data;
    SqlDataReader dataReader;
    String sql = "";

    sql = "SELECT TOP(1) start_rg, end_rg FROM Rg WHERE status = 'Avail' ";
    command_Data = new SqlCommand(sql, cnn);

    dataReader = command_Data.ExecuteReader();

    var fileName = folderName.Substring(0, 4);
    string Path = $@"G:\Eng\Share\PC\{foldername}\SSD\SN\{fileName}_DataFile.txt";


    using (StreamWriter tw = File.CreateText(Path))
    {
        while (dataRead.Read())
        {
            tw.WriteLine("RG");
            tw.WriteLine("StartRG={0}", dataRead["start_rg"]);
            tw.WriteLine("EndRG={0}", dataRead["end_rg"]);
        }
    }
    dataReader.Close();
    command_Data.Dispose();
    
}

From what I can tell from method with comboboxes, similair logic can be applied to this huge if, and all code could be grestly simplified. Also, I guess you want to take 5 characters from folder name, but it is up to you:

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    var folderName = comboBox2.SelectedItem.ToString();
    var fileName = folderName.Substring(0, 4);
    var Path = $@"G:\Eng\Share\PC\{foldername}\SSD\SN\{fileName}_DataFile.txt";
    var logPath = $@"G:\Eng\Share\PC\{foldername}\SSD\SystemLog\SystemLog_{DateTime.Now:yyyyMMdd-HHmm}.txt";
    
    generateFile(folderName);
}
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
0

You could just pass the Path parameter into the generateFile() function directly.

You can also assign the folder variable directly into the string instead of using a lot of if-else statements to add the hard coded path for each variant.

Example:

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    if(comboBox2.SelectedItem is string folder 
        && !string.IsNullOrEmpty(folder)) 
    {
        // Get FolderName (Get Part of the Folder Name after '-')
        string folderName = folder.Split('-')[1];
    
        // Insert variables into the folderPath
        string path = $@"G:\Eng\Share\PC\{folder}\SSD\SN\{folderName}_DataFile.txt";
    
        // Insert variables into the logPath
        string logPath = $@"G:\Eng\Share\PC\{folder}\SSD\SystemLog
            + \SystemLog_{DateTime.Now:yyyyMMdd-HHmm}.txt";
    
        generateFile(path);
    }
}

generateFile Method:

private void generateFile(string Path)
{
    .....
}
IndieGameDev
  • 2,905
  • 3
  • 16
  • 29