读文件 
public class FileClass 
  { 
   public static void Main() 
   { 
   ReadFromFile("c:\MyTextFile.txt"); 
   } 
   static void ReadFromFile(string filename) 
   { 
   StreamReader SR; 
   string S; 
   SR=File.OpenText(filename); 
   S=SR.ReadLine(); 
   while(S!=null) 
   { 
   Console.WriteLine(S); 
   S=SR.ReadLine(); 
   } 
   SR.Close(); 
   } 
  } 

解决方案 »

  1.   

    我要写到B/S上.上面的代码好像在C/S的吧.能不能写到B/S上呢?我刚学C#.NET
      

  2.   

    我试了,但是有错误.说文件找不到.
    ReadFromFile(@"c:\MyTextFile.txt");
    我在c:\建了这个文件
      

  3.   

    将Console.WriteLine(S); 改成你要处理S的过程,例如在页面上显示S
    就行啦
      

  4.   

    对于web,你需要有写入权限,二、相对路径 ReadFromFile(Server.MapPath+"MyTextFile.txt");
      

  5.   

    你的web项目不能够直接访问C:盘的
      

  6.   

    MapPath  这个是在哪个类里面的.
      

  7.   

    using(FileStream fs=File.Create(strpath)){};
    using(FileStream fs=File.OpenWrite(strpath))
    {
    sw.WriteLine("20380000 4360000 20535000 4490000");
    sw.WriteLine("130 155");
    sw.Close();
    }
    using (  StreamReader srr = new StreamReader(pathout+strmodname+stryear+i.ToString()+".txt"))
    { while ((line = srr.ReadLine()) != null&&mm<151) 
    {
    DataRow dr=dt.NewRow();//增加新行,把txt的一行数据载入
    string[] strGroup=line.Split(new Char []{'\t'});//给dt里得行赋值
    dr[0]=strGroup[0];
    dr[1]=int.Parse(strGroup[1]);
    dr[2]=int.Parse(strGroup[2]);
    dr[3]=strGroup[3];
    dt.Rows.Add(dr);
    }
    }
      

  8.   

    如果要在文件后面追加字串可以用
    using(StreamWriter  sw=File.AppendText(path))
    {
    while((line=sr.ReadLine())!=null)

     {
    ....


     }
    }
      

  9.   

    楼主是不是想做脚本编译器呀?如果是要执行脚本代码的话,可以用VSA
      

  10.   

    VSA是什么东西啊!不懂.我只是想把脚本里的数据拿出来.保存到数据库里
      

  11.   

    能说的具体点吗?本人是刚学.NET!!!!谢谢
      

  12.   

    FileStream f  = new FileStream(filepath,FileMode.OpenOrCreate);
    StreamReader sdr = new StreamReader(f);
    while ((line = sdr.ReadLine()) != null)
    {
    connStr = line;
    }
      

  13.   

    根据我的经验,web应用是可以直接用c:\xxx.xx的方式访问文件的,但一定要给你的应用程序池启动帐号赋权限。默认是network service
      

  14.   

    C#追加文件 
    StreamWriter sw = File.AppendText(Server.MapPath(".")+"\\myText.txt"); 
    sw.WriteLine("追逐理想"); 
    sw.WriteLine("kzlll"); 
    sw.WriteLine(".NET笔记"); 
    sw.Flush(); 
    sw.Close(); C#拷贝文件 
    string OrignFile,NewFile; 
    OrignFile = Server.MapPath(".")+"\\myText.txt"; 
    NewFile = Server.MapPath(".")+"\\myTextCopy.txt"; 
    File.Copy(OrignFile,NewFile,true); C#删除文件 
    string delFile = Server.MapPath(".")+"\\myTextCopy.txt"; 
    File.Delete(delFile); C#移动文件 
    string OrignFile,NewFile; 
    OrignFile = Server.MapPath(".")+"\\myText.txt"; 
    NewFile = Server.MapPath(".")+"\\myTextCopy.txt"; 
    File.Move(OrignFile,NewFile); C#创建目录 
    // 创建目录c:\sixAge 
    DirectoryInfo d=Directory.CreateDirectory("c:\\sixAge"); 
    // d1指向c:\sixAge\sixAge1 
    DirectoryInfo d1=d.CreateSubdirectory("sixAge1"); 
    // d2指向c:\sixAge\sixAge1\sixAge1_1 
    DirectoryInfo d2=d1.CreateSubdirectory("sixAge1_1"); 
    // 将当前目录设为c:\sixAge 
    Directory.SetCurrentDirectory("c:\\sixAge"); 
    // 创建目录c:\sixAge\sixAge2 
    Directory.CreateDirectory("sixAge2"); 
    // 创建目录c:\sixAge\sixAge2\sixAge2_1 
    Directory.CreateDirectory("sixAge2\\sixAge2_1"); 递归删除文件夹及文件 
    <%@ Page Language=C#%> 
    <%@ Import namespace="System.IO"%> 
    <Script runat=server> 
    public void DeleteFolder(string dir) 

    if (Directory.Exists(dir)) //如果存在这个文件夹删除之 

    foreach(string d in Directory.GetFileSystemEntries(dir)) 

    if(File.Exists(d)) 
    File.Delete(d); //直接删除其中的文件 
    else 
    DeleteFolder(d); //递归删除子文件夹 

    Directory.Delete(dir); //删除已空文件夹 
    Response.Write(dir+" 文件夹删除成功"); 

    else 
    Response.Write(dir+" 该文件夹不存在"); //如果文件夹不存在则提示 

    protected void Page_Load (Object sender ,EventArgs e) 

    string Dir="D:\\gbook\\11"; 
    DeleteFolder(Dir); //调用函数删除文件夹 
    }  http://blog.csdn.net/guanshenglang/archive/2007/01/26/1494447.aspx