这是一个文件类型检测的函数
private bool IsAllowedExtension(FileUpload hifile)
    {
        //FileUpload hifile = (FileUpload)this.FindControl(Filee);
        System.IO.FileStream fs = new System.IO.FileStream(hifile.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
//未能找到文件“G:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\xxxx.jpg”。
  //为什么这个路径会跑到安装目录下去了,而不是我选择文件的路径?
        System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
        string fileclass = "";
        byte buffer;
        try
        {
            buffer = r.ReadByte();
            fileclass = buffer.ToString();
            buffer = r.ReadByte();
            fileclass += buffer.ToString();
        }
        catch
        {        }
        r.Close();
        fs.Close();
        if (fileclass == "255216" || fileclass == "7173")
        //说明255216是jpg;               
        {
            return true;
        }
        else
        {
            return false;
        }
    }

解决方案 »

  1.   

     string fileName = fuMessageContent.FileName;
                string type = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();
                if (type != "txt")
                {                this.LblFile.Text = "请选择txt类型的文本文件";
                    return;
                }
                else
                {
                    this.LblFile.Text = "";
                }            Stream stream = this.fuMessageContent.FileContent;
                StreamReader reader = new StreamReader(stream, System.Text.Encoding.GetEncoding("gb2312"));
                string contents = "";
                while (!reader.EndOfStream)
                {
                    contents = reader.ReadToEnd();            }
                this.txtMessageContent.Text = contents;
                reader.Close();
                stream.Close();
      

  2.   

     System.IO.FileStream fs = new System.IO.FileStream(hifile.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
    //未能找到文件“G:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\xxxx.jpg”。
      //为什么这个路径会跑到安装目录下去了,而不是我选择文件的路径?