关于生成文件的问题

th1986 2015-08-28 09:29:20
利用C#生成一个文件,文件格式是文本(txt),最后的生成名是 XXX.xx.xxxxx.CQD

XXX是 Textbox1里输入的内容
xx 是 Textbox2里输入的内容
尾缀是 .CQD

如何生成?
...全文
132 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
th1986 2015-08-31
  • 打赏
  • 举报
回复
string path = @"c:\temp\" + Text1.Text + "." + Text2.Text + ".CQD"; 生成的文件应该是 1111.2222.CQD.txt string path = @"c:\temp\" + Text1.Text + "." + Text2.Text + "+"."+"+CQD+"."txt"; 如何给CQD赋值?
th1986 2015-08-28
  • 打赏
  • 举报
回复
引用 5 楼 findcaiyzh 的回复:
这个自己拼接下字符串就可以了吧 类似这样 string path = @"c:\temp\" + Text1.Text + "." + Text2.Text + ".CQD";
感谢...
_lee_chong 2015-08-28
  • 打赏
  • 举报
回复
...直接字符串一拼接然后创建文件不就成了
Imcx 2015-08-28
  • 打赏
  • 举报
回复
public static void DataSave(string saveDirectory, string saveName, string saveContent) { string filePath = saveDirectory + saveName + ".cqd"; if (!Directory.Exists(saveDirectory)) { Directory.CreateDirectory(saveDirectory); } using (StreamWriter sw = File.AppendText(filePath)) { sw.Write(saveContent); } }
宝_爸 2015-08-28
  • 打赏
  • 举报
回复
这个自己拼接下字符串就可以了吧 类似这样 string path = @"c:\temp\" + Text1.Text + "." + Text2.Text + ".CQD";
th1986 2015-08-28
  • 打赏
  • 举报
回复
Textbox1里 输入的字符,怎么显示在生成的CQD文件中? 代码里好像没输出吧
th1986 2015-08-28
  • 打赏
  • 举报
回复
兄弟啊,你这个是生成文件了,但是没给我Textbox1里输入的内容生成出来啊 比如Textbox1里输入 大连 ,我要生成的是大连.CQD
Imcx 2015-08-28
  • 打赏
  • 举报
回复
按楼上的,把路径里的文件名改成cqd结尾
宝_爸 2015-08-28
  • 打赏
  • 举报
回复
File.Create Method public static FileStream Create( string path ) Creates or overwrites a file in the specified path.

using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        try
        {

            // Delete the file if it exists. 
            if (File.Exists(path))
            {
                // Note that no lock is put on the 
                // file and the possibility exists 
                // that another process could do 
                // something with it between 
                // the calls to Exists and Delete.
                File.Delete(path);
            }

            // Create the file. 
            using (FileStream fs = File.Create(path))
            {
                Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
                // Add some information to the file.
                fs.Write(info, 0, info.Length);
            }

            // Open the stream and read it back. 
            using (StreamReader sr = File.OpenText(path))
            {
                string s = "";
                while ((s = sr.ReadLine()) != null)
                {
                    Console.WriteLine(s);
                }
            }
        }

        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}
以上来自: https://msdn.microsoft.com/en-us/library/d62kzs03(v=vs.110).aspx
smthgdin_020 2015-08-28
  • 打赏
  • 举报
回复
cqd是什么文件类型?比如说按照格式生成个txt后再改文件类型?
th1986 2015-08-28
  • 打赏
  • 举报
回复
string path = @"c:\temp\" + Text1.Text + "." + Text2.Text + ".CQD"; 这样直接输出生成文件,好像有错误吧

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧