簡體   English   中英

詢問位置以保存使用C#創建的文本文件

[英]Ask location to save text file created using c#

我正在將datagridview數據導出為文本文件格式,我嘗試了以下代碼

        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);
        }

在這里,數據已完美導出,並以文本文件格式保存,問題是我在這里分配了默認位置,而不是此位置,它應該通過打開保存對話框來詢問位置。

您正在尋找saveFileDialog 這是一個教程

例:

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;

您應該使用SaveFileDialog

        SaveFileDialog sfd = new SaveFileDialog();

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM