Example #1
0
        public virtual Stream Open(IServiceContainer services, string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException("fileName");
            }
            WriterDebugger debugger = ( WriterDebugger )services.GetService(typeof(WriterDebugger));
            //if( debugger != null && debugger.Enabled )
            //{
            //    debugger.WriteLine( string.Format(
            //        WriterStrings.Loading_FileName ,
            //        fileName ));
            //}
            UrlStream stream = UrlStream.Open(fileName);

            //if (debugger != null)
            //{
            //    if (stream == null)
            //    {
            //        debugger.WriteLine(WriterStrings.Fail);
            //    }
            //    else
            //    {
            //        debugger.WriteLine(string.Format(
            //            WriterStrings.LoadComplete_Size,
            //            WriterUtils.FormatByteSize( (int)stream.Length)));
            //    }
            //}
            return(stream);
        }
Example #2
0
        /// <summary>
        /// 打开文件流
        /// </summary>
        /// <param name="url">URL地址</param>
        /// <returns>创建的流对象</returns>
        public static UrlStream Open(string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("url");
            }
            url = url.Trim();
            if (url.Length == 0)
            {
                throw new ArgumentNullException("url");
            }

            if (url.IndexOf(":") > 0)
            {
                Uri uri = new Uri(url);
                if (uri.Scheme == Uri.UriSchemeFile)
                {
                    string fn = uri.LocalPath;
                    if (System.IO.File.Exists(fn) == false)
                    {
                        throw new System.IO.FileNotFoundException(fn);
                    }
                    UrlStream stream = new UrlStream();
                    stream._BaseStream = new System.IO.FileStream(
                        fn,
                        FileMode.Open,
                        FileAccess.Read);
                    return(stream);
                }
                else
                {
                    using (System.Net.WebClient client = new System.Net.WebClient())
                    {
                        //Uri uri2 = new Uri(url);
                        byte[]    bs     = client.DownloadData(url);
                        UrlStream stream = new UrlStream();
                        stream._BaseStream = new System.IO.MemoryStream(bs);
                        return(stream);
                    }
                }
            }
            else
            {
                // 可能是相对路径,读取本地文件路径
                string    fn     = System.IO.Path.Combine(System.Environment.CurrentDirectory, url);
                UrlStream stream = new UrlStream();
                stream._BaseStream = new System.IO.FileStream(
                    fn,
                    FileMode.Open,
                    FileAccess.Read);
                return(stream);
            }
            //return null;
        }
Example #3
0
        public virtual Stream Save(IServiceContainer services, string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException("fileName");
            }
            UrlStream stream = UrlStream.Save(fileName);

            //WriterDebugger debugger = (WriterDebugger)services.GetService(typeof(WriterDebugger));
            //if (debugger != null && debugger.Enabled)
            //{
            //    debugger.WriteLine(string.Format(
            //        WriterStrings.Saving_FileName ,
            //        fileName));
            //}
            return(stream);
        }
Example #4
0
        /// <summary>
        /// 打开保存数据的流
        /// </summary>
        /// <param name="url">URL字符串</param>
        /// <returns>创建的流对象</returns>
        public static UrlStream Save(string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("url");
            }
            url = url.Trim();
            if (url.Length == 0)
            {
                throw new ArgumentNullException("url");
            }

            if (url.IndexOf(":") > 0)
            {
                Uri uri = new Uri(url);
                if (uri.Scheme == Uri.UriSchemeFile)
                {
                    string    fn     = uri.LocalPath;
                    UrlStream stream = new UrlStream();
                    stream._BaseStream = new System.IO.FileStream(
                        fn,
                        FileMode.Create,
                        FileAccess.Write);
                    return(stream);
                }
                else
                {
                    UrlStream stream = new UrlStream();
                    stream._BaseStream = new System.IO.MemoryStream();
                    stream._SaveUrl    = url;
                    return(stream);
                }
            }
            else
            {
                // 可能是相对路径,读取本地文件路径
                string    fn     = System.IO.Path.Combine(System.Environment.CurrentDirectory, url);
                UrlStream stream = new UrlStream();
                stream._BaseStream = new System.IO.FileStream(
                    fn,
                    FileMode.Create,
                    FileAccess.Write);
                return(stream);
            }
        }