在Windows应用程序中创建文本文件 [英] Create a text file in windows application

查看:65
本文介绍了在Windows应用程序中创建文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打开一个文本文件并将一些数据写入该文件.这是我的代码:

I want to open a text file and write some data to the file. Here is my code:

        FileStream fs1 = new FileStream("D:\\Yourfile.txt", FileMode.OpenOrCreate, FileAccess.Write);
        StreamWriter writer = new StreamWriter(fs1);
        writer.Write("Hello");
        writer.Close();
         System.Diagnostics.Process.Start(@"D:\\Yourfile.txt");

此代码可以正常工作,但是首先要保存文件.我希望文本文件与数据一起打开,并让用户保存该文本文件.有可能吗?

This code works fine.But here first the file is getting saved. I want a text file to open along with the data and let the user save the text file. Is it possible?

推荐答案

请尝试解决您的问题

        [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        [DllImport("User32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
        private void button6_Click(object sender, EventArgs e)
        {        

            Process[] notepads = Process.GetProcessesByName("notepad");
            if (notepads.Length == 0)
            {
                Process.Start(@"notepad.exe");
                Thread.Sleep(100);
            }
            notepads = Process.GetProcessesByName("notepad");
               // return;
            if (notepads[0] != null)
            {
                IntPtr child = FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null);
                SendMessage(child, 0x000C, 0, "Hello");
            }              

        }

这篇关于在Windows应用程序中创建文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆