简体   繁体   English

将TextBox的内容导出到文本文件Visual Studio C#

[英]Exporting contents of a TextBox to a Text File Visual Studio C#

I am trying to export the contents of a textbox to a txt file at the press of a button "Export". 我试图通过按下“导出”按钮将文本框的内容导出到txt文件。 I am using web application forms in Visual Studio in C#. 我在C#中使用Visual Studio中的Web应用程序表单。

I have got the txt file creation part working at the press of a button. 我按下一个按钮就可以使用txt文件创建部分。 But I am unable to export the data from the textbox into the text file. 但我无法将文本框中的数据导出到文本文件中。

The textbox's contents are referencing a DataGrid so how can I link the program in C# so that at the press of the button "Export" it transfers the contents fo the textbox into the text file? 文本框的内容是引用DataGrid的,所以如何在C#中链接程序,以便在按下“导出”按钮时将文本框的内容传输到文本文件中?

So the code below is what I have to create a text file. 所以下面的代码是我创建文本文件的代码。 What would I need to add to this, to do the above? 为了做到这一点,我还需要添加什么呢?

    private void Export_Click(object sender, EventArgs e)
    {
         //DataRowView drv = ((DataRowView)ordersBindingSource.Current);
         //DataRow dr = drv.Row;        

        string path = @"G:\bin\Debug\Test.txt";
        if (!File.Exists(path))
        {
            // Create a file to write to. 
            using (StreamWriter sw = File.CreateText(path))
            {
                sw.WriteLine("Hi," + System.Environment.NewLine);
                sw.WriteLine("Order ID: ");
            }
        }
    }

Please Help!! 请帮忙!! I am working on a project and stuck at this level of exporting information. 我正在开展一个项目,并坚持这一级别的出口信息。

Put your textbox in a form 将您的文本框放在一个表单中
Then set the Export button to be a submit button (also in the form) 然后将“导出”按钮设置为提交按钮(也在表单中)

On the server, when the post is done, you'll be able to access to the textbox content ( this.Forms if I'm not wrong, it's a long time I didn't use asp.net) 在服务器上,当帖子完成后,你将能够访问文本框内容( this.Forms如果我没有错,很长一段时间我没有使用asp.net)

Once you have the textbox content, you just need to use 获得文本框内容后,您只需使用即可

System.IO.File.WriteAllText(@"G:\bin\Debug\Test.txt", textBoxContent)

Be carefull: Asp.net doesn't have the permissions to write anywhere on the disk, you may need to give permission on a specific directory 注意:Asp.net没有在磁盘上任何地方写入的权限,您可能需要授予特定目录的权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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