求asp。net写文件的代码段例子,我参考一下就可以

jourmen 2004-04-10 11:33:30
要求很简单,
1.创建一个文本文件
2.打开文件
3.写入一段字符串
4.关闭文件
这样就可以了

谢谢呀:)
...全文
74 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
jourmen 2004-04-10
  • 打赏
  • 举报
回复
谢谢!
散分:)
活靶子哥哥 2004-04-10
  • 打赏
  • 举报
回复
向文件写入文本 [C#]请参见
创建目录列表 | 对刚创建的数据文件进行读取和写入 | 打开并附加到日志文件 | 从文件读取文本 | 从字符串中读取字符 | 向字符串写入字符 | 基本的文件 I/O | StreamWriter | File.CreateText
语言
C#

Visual Basic

全部显示
下面的代码示例显示向文本文件写入文本的简单方法。

[Visual Basic]
Imports System
Imports System.IO

Class Test
Public Shared Sub Main()
' Create an instance of StreamWriter to write text to a file.
Dim sw As StreamWriter = New StreamWriter("TestFile.txt")
' Add some text to the file.
sw.Write("This is the ")
sw.WriteLine("header for the file.")
sw.WriteLine("-------------------")
' Arbitrary objects can also be written to the file.
sw.Write("The date is: ")
sw.WriteLine(DateTime.Now)
sw.Close()
End Sub
End Class

[C#]
using System;
using System.IO;

class Test
{
public static void Main()
{
// Create an instance of StreamWriter to write text to a file.
// The using statement also closes the StreamWriter.
using (StreamWriter sw = new StreamWriter("TestFile.txt"))
{
// Add some text to the file.
sw.Write("This is the ");
sw.WriteLine("header for the file.");
sw.WriteLine("-------------------");
// Arbitrary objects can also be written to the file.
sw.Write("The date is: ");
sw.WriteLine(DateTime.Now);
}
}
}

下面的代码示例创建一个新文本文件并向其写入一个字符串。

[Visual Basic]
Option Explicit On
Option Strict On
Imports System
Imports System.IO
Public Class TextToFile
Private Const FILE_NAME As String = "MyFile.txt"
Public Shared Sub Main()
If File.Exists(FILE_NAME) Then
Console.WriteLine("{0} already exists.", FILE_NAME)
Return
End If
Dim sr As StreamWriter = File.CreateText(FILE_NAME)
sr.WriteLine("This is my file.")
sr.WriteLine("I can write ints {0} or floats {1}, and so on.", 1, 4.2)
sr.Close()
End Sub
End Class

[C#]
using System;
using System.IO;
public class TextToFile
{
private const string FILE_NAME = "MyFile.txt";
public static void Main(String[] args)
{
if (File.Exists(FILE_NAME))
{
Console.WriteLine("{0} already exists.", FILE_NAME);
return;
}
StreamWriter sr = File.CreateText(FILE_NAME);
sr.WriteLine ("This is my file.");
sr.WriteLine ("I can write ints {0} or floats {1}, and so on.",
1, 4.2);
sr.Close();
}
}
孟子E章 2004-04-10
  • 打赏
  • 举报
回复
//文件保存的物理路径,CSTest为虚拟目录名称,F:\Inetpub\wwwroot\CSTest为物理路径
string p = @"F:\Inetpub\wwwroot\CSTest";
//我们在虚拟目录的根目录下建立SchedulerJob文件夹,并设置权限为匿名可修改,
//SchedulerJob.txt就是我们所写的文件
string FILE_NAME = p+ "\\SchedulerJob\\SchedulerJob.txt";
//取得当前服务器时间,并转换成字符串
string c = System.DateTime.Now.ToString("yyyy-mm-dd hh:MM:ss");
//标记是否是新建文件的标量
bool flag = false;
//如果文件不存在,就新建该文件
if (!File.Exists(FILE_NAME))
{
flag = true;
StreamWriter sr = File.CreateText(FILE_NAME);
sr.Close();
}
//向文件写入内容
StreamWriter x = new StreamWriter(FILE_NAME,true,System.Text.Encoding.Default);
if(flag) x.Write("计划任务测试开始:");
x.Write("\r\n"+c);
x.Close();
活靶子哥哥 2004-04-10
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2945/2945829.xml?temp=.6093561

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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