简体   繁体   中英

Ask location to save text file created using c#

i am exporting datagridview data into format of text file, i tried the below code

        string dirLocationString = @"C:\Users\palanar\Desktop\result.txt";
        StreamWriter sW = new StreamWriter(dirLocationString);
        string lines = "";
        lines = "DateTime" + "\t" + "TestStatus" + "\t" + "BatPackSN" + "\t" + "CellSN"
                + "\t" + "BlockSN" + "\t" + "UI_ID" + "\t" + "UI_ParentID" + "\t" + "OperationNumber"
                + "\t" + "OperationName" + "\t" + "EquipmentNumber" + "\t" + "EquipmentName" + "\t" + "WorkOrder"
                + "\t" + "Assembly" + "\t" + "ProductName" + "\t" + "HandlingDuration" + "\t" + "OperationDuration"
                + "\t" + "RepairID" + "\t" + "DefectID" + "\t" + "UitemLevelCode" + "\t" + "UIEventLevelCode";
        sW.WriteLine(lines);
        for (int row = 0; row < dataGridView2.Rows.Count - 1; row++)
        {
            string lines1 = "";
            for (int col = 0; col <= 19; col++)
            {
                lines1 += (string.IsNullOrEmpty(lines1) ? " " : "\t") + dataGridView2.Rows[row].Cells[col].Value.ToString();
            }

            sW.WriteLine(lines1);
        }

here the data is exported perfectly and saved in the format of text file, the problem is here i have assigned default location, instead of this it should ask for the location by opening save dialogue.

you are looking for saveFileDialog this is a tutorial

Example:

SaveFileDialog sfd = new SaveFileDialog();

sfd.Filter = "Text file(*.txt)|*.txt";
sfd.FilterIndex = 1;

if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) { return; }
string dirLocationString = sfd.FileName;

you should use SaveFileDialog

        SaveFileDialog sfd = new SaveFileDialog();

        if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            //your selected file location 
            string dirLocationString = sfd.FileName;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM