如何引用文本框中包含的文本来创建 XML 文件?

发布于 2024-12-14 13:25:28 字数 1535 浏览 3 评论 0原文

我正在使用 Visual C++ 2005 .net,我希望从此表单创建一个文件。我希望能够通过输入文本框中的内容来命名该文件。任何帮助将不胜感激。

这是形式:

在此处输入图像描述

这是我到目前为止所拥有的:

  private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

        String^ path = "C:\\" ".txt" ;

        StreamWriter^ sw = File::CreateText( path );

        try
          {
             sw->WriteLine("");

          }
          finally
          {
             if ( sw )
                      delete (IDisposable^)sw;
          }

                 }
        };

固定代码:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {               

     NewPart ^newpart = gcnew NewPart();
                     newpart->ShowDialog();
                     this->Close();

                    String^ fileName = textBox1->Text;
                    String^ filetype = ".xml";
                    String^ path = L"C:\\;

                     String^ fullName = path + fileName + filetype;

                    StreamWriter^ sw = File::CreateText(fullName);

        try
          {
             sw->WriteLine("<?xml version= \"1.0\" standalone=\"yes\"?>");
             sw->WriteLine("<macdata");
             sw->WriteLine("</macdata>");

          }
          finally
          {
             if ( sw )
                      delete (IDisposable^)sw;
          }

                 }


        };
    }

I am using visual c++ 2005 .net and I am looking to create a file from this form. I want to be able to name the file by entering what ever is in the text box. Any help would be greatly appreciated.

Here is the form:

enter image description here

this is what I have so far:

  private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

        String^ path = "C:\\" ".txt" ;

        StreamWriter^ sw = File::CreateText( path );

        try
          {
             sw->WriteLine("");

          }
          finally
          {
             if ( sw )
                      delete (IDisposable^)sw;
          }

                 }
        };

FIXED CODE:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {               

     NewPart ^newpart = gcnew NewPart();
                     newpart->ShowDialog();
                     this->Close();

                    String^ fileName = textBox1->Text;
                    String^ filetype = ".xml";
                    String^ path = L"C:\\;

                     String^ fullName = path + fileName + filetype;

                    StreamWriter^ sw = File::CreateText(fullName);

        try
          {
             sw->WriteLine("<?xml version= \"1.0\" standalone=\"yes\"?>");
             sw->WriteLine("<macdata");
             sw->WriteLine("</macdata>");

          }
          finally
          {
             if ( sw )
                      delete (IDisposable^)sw;
          }

                 }


        };
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘然心甜 2024-12-21 13:25:28
String^ path = textBox1->Text;

将 textBox1 替换为表单中的实际 TextBox 名称。

编辑。关于第二个问题,请看这段代码:

int main(array ^args)
{
    String^ fileName = L"file.txt";
    String^ path = L"C:\\";

    String^ fullName = path + fileName;

    Console::WriteLine(fullName);

    return 0;
}
String^ path = textBox1->Text;

Replace textBox1 with actual TextBox name from your form.

Edit. Regarding your second question, look at this code:

int main(array ^args)
{
    String^ fileName = L"file.txt";
    String^ path = L"C:\\";

    String^ fullName = path + fileName;

    Console::WriteLine(fullName);

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文