/*
         * Helper method to open a file from its virtual path
         */

        public static Stream OpenFile(string virtualPath)
        {
            VirtualPathProvider vpathProvider = HostingEnvironment.VirtualPathProvider;
            VirtualFile         vfile         = vpathProvider.GetFileWithCheck(virtualPath);

            return(vfile.Open());
        }
Exemple #2
0
        public static Stream OpenFile(string virtualPath)
        {
            // This thing throws a nullref when we're not inside an ASP.NET appdomain, which is what MS does.
            VirtualPathProvider provider = HostingEnvironment.VirtualPathProvider;
            VirtualFile         file     = provider.GetFile(virtualPath);

            if (file != null)
            {
                return(file.Open());
            }

            return(null);
        }
        private string[] _GetDependencies(VirtualFile virtualFile)
        {
            var dir = VirtualPathUtility.GetDirectory(virtualFile.VirtualPath);

            string content;
            using (var stream = virtualFile.Open())
            using (var reader = new StreamReader(stream))
                content = reader.ReadToEnd();

            return _ReferenceRegex.Matches(content).Cast<Match>().Select(m => {
                var relativePath = m.Groups["path"].Value;
                return VirtualPathUtility.Combine(dir, relativePath);
            }).Where(m => this.ExcludedDependencies.All(e => !m.Contains(@"/" + e))).ToArray();
        }
Exemple #4
0
        private string GetPageTitle(VirtualFile file)
        {
            string aspx;
            using (var infile = file.Open())
            {
                using (var reader = new StreamReader(infile))
                {
                    aspx = reader.ReadToEnd();
                }
            }

            var m = titleRegex.Match(aspx);
            if (m.Success && m.Groups.Count > 1)
            {
                return m.Groups[1].Value;
            }
            else
            {
                return VirtualPathUtility.GetFileName(file.VirtualPath);
            }
        }
		internal void TransmitFile (VirtualFile vf, bool final_flush)
		{
			if (vf == null)
				throw new ArgumentNullException ("vf");

			if (vf is DefaultVirtualFile) {
				TransmitFile (HostingEnvironment.MapPath (vf.VirtualPath), final_flush);
				return;
			}
			
			byte[] buf = new byte [bufLen];
			using (Stream s = vf.Open ()) {
				int readBytes;
				while ((readBytes = s.Read (buf, 0, bufLen)) > 0) {
					output_stream.Write (buf, 0, readBytes);
					output_stream.ApplyFilter (final_flush);
					Flush (false);
				}
				if (final_flush)
					Flush (true);
			}
		}
 private IEnumerable<string> ReadLines(VirtualFile file)
 {
     using(var s = file.Open())
     using (var sr = new StreamReader(s))
     {
         while(sr.Peek() >= 0)
         {
             yield return sr.ReadLine();
         }
     }
 }
 internal void WriteVirtualFile(VirtualFile vf)
 {
     using (Stream stream = vf.Open())
     {
         if (this.UsingHttpWriter)
         {
             long length = stream.Length;
             if (length > 0L)
             {
                 byte[] buffer = new byte[(int) length];
                 int count = stream.Read(buffer, 0, (int) length);
                 this._httpWriter.WriteBytes(buffer, 0, count);
             }
         }
         else
         {
             this.WriteStreamAsText(stream, 0L, -1L);
         }
     }
 }
 /// <summary>
 /// 从指定虚拟文件中读取文本内容
 /// </summary>
 /// <param name="file">虚拟文件</param>
 /// <returns></returns>
 public static string LoadContent( VirtualFile file )
 {
   using ( var reader = new StreamReader( file.Open(), true ) )
   {
     return reader.ReadToEnd();
   }
 }
        public static Stream Open(VirtualFile virtualFile)
        {
            using (var fs = virtualFile.Open())
            {
                var stream = new MemoryStream();
                fs.CopyTo(stream);
                stream.Position = 0;
                return stream;
            }

            /*
            using (var fs = File.OpenRead(path))
            {
                var stream = new MemoryStream();
                fs.CopyTo(stream);
                stream.Position = 0;
                return stream;
            }
             * */
        }
        // support for VirtualPathProvider
        internal void WriteVirtualFile(VirtualFile vf) {
            Debug.Trace("WriteVirtualFile", vf.Name);

            using (Stream s = vf.Open()) {
                if (UsingHttpWriter) {
                    long size = s.Length;

                    if (size > 0) {
                        // write as memory block
                        byte[] fileBytes = new byte[(int)size];
                        int bytesRead = s.Read(fileBytes, 0, (int) size);
                        _httpWriter.WriteBytes(fileBytes, 0, bytesRead);
                    }
                }
                else {
                    // Write file contents
                    WriteStreamAsText(s, 0, -1);
                }
            }
        }