zoukankan      html  css  js  c++  java
  • ASP.NET在本地服务器上创建目录并在该目录下写文件(转)

    代码
    public static Boolean WriteTextFile(string content, string filepath,string name)
        {
            FileStream fs;
            StreamWriter sw;
            
    if (!System.IO.Directory.Exists(filepath))
            {
              DirectoryInfo DirInfo 
    = Directory.CreateDirectory(filepath); //创建目录
               DirInfo.Attributes = FileAttributes.Normal; 
            }
            
    string fullpath = filepath  + name + ".txt";
            
    if (File.Exists(fullpath))//验证文件是否存在
                {
                    
    return false;
                }
                
    else
                {
                    fs 
    = new FileStream(fullpath, FileMode.Create, FileAccess.Write);
                    sw 
    = new StreamWriter(fs,System.Text.Encoding.Default);
                    sw.WriteLine(content);
                    sw.Close();
                    fs.Close();
                    
    return true;

                }
        }

    在做这个相关工作的时候 还碰到一个问题就是 用一下方法读取中文文本文件时,出现乱码的现象:

    代码
    public static String ReadTextFile(string filepath)   
        {   
      
      
            StreamReader objReader 
    = new StreamReader(filepath);   
            
    string sLine = "";   
            ArrayList arrText 
    = new ArrayList();   
      
            
    while (sLine != null)   
            {   
                sLine 
    = objReader.ReadLine();   
                
    if (sLine != null)   
                    arrText.Add(sLine);   
            }   
            objReader.Close();   
            StringBuilder sb 
    = new StringBuilder();   
      
            
    foreach (string sOutput in arrText)   
                sb.Append(sOutput);   
      
            
    return sb.ToString();   
        }  

    查询资料 发现对上面代码中的StreamReader修改一下,如下代码:

    代码
    public static String ReadTextFile(string filepath)   
       {   
      
      
           StreamReader objReader 
    = new StreamReader(filepath, System.Text.Encoding.Default);   
           
    string sLine = "";   
           ArrayList arrText 
    = new ArrayList();   
      
           
    while (sLine != null)   
           {   
               sLine 
    = objReader.ReadLine();   
               
    if (sLine != null)   
                   arrText.Add(sLine);   
           }   
           objReader.Close();   
           StringBuilder sb 
    = new StringBuilder();   
      
           
    foreach (string sOutput in arrText)   
               sb.Append(sOutput);   
      
           
    return sb.ToString();   
       }  

    则没有出现乱码的情况,同理 在写文件的时候,也要这样写:

    sw = new StreamWriter(fs,System.Text.Encoding.Default);

    如果这样写:

    sw = new StreamWriter(fs);

    能够写出新的文件,在windows里面打开也和普通文件没有差别,但是在读取的时候会出现认不出中文的情况.这个问题在全文检索系统中困扰了一阵子,就是用sw = new StreamWriter(fs);写进去的文章 检索不出来,可能就是编码不匹配造成的

  • 相关阅读:
    ASP字符串16进制互转
    LINQ Dictionary加速查询(TryGetValue)
    ASP中将UTF8格式的URL进行解码。函数
    C# 保护进程不被结束(源代码)防任务管理器结束进程
    用WebBrowser做web 打印时的权限不足。。。。
    .net 2.0 中 ‘注册为 allowDefinition='MachineToApplication' 的节是错误的’
    sql 2005 中分页
    BitComet web 插件和 flash get web 插件 对脚本的影响
    同时使用 C# and VB.NET 在 .net 2.0 中
    用触发器实现主从表关系(主表更改从表更改 )
  • 原文地址:https://www.cnblogs.com/lann/p/1641272.html
Copyright © 2011-2022 走看看