相关疑难解决方法(0)

你能让StreamReader不要处理底层流吗?

有没有办法做到这一点:

this.logFile = File.Open("what_r_u_doing.log", FileMode.OpenOrCreate, FileAccess.ReadWrite);

using(var sr = new StreamReader(this.logFile))
{
    // Read the data in
}

// ... later on in the class ...

this.logFile = File.Open("what_r_u_doing.log", FileMode.OpenOrCreate, FileAccess.ReadWrite);

using(var sw = new StreamWriter(this.logFile))
{
    // Write additional data out...
}
Run Code Online (Sandbox Code Playgroud)

无需打开文件两次?

我似乎无法使StreamReader不处理我的流.我也不想让它超出范围.然后垃圾收集器最终将调用Dispose,杀死流.

c# file-io

49
推荐指数
3
解决办法
2万
查看次数

无法访问memoryStream的封闭Stream,如何重新打开?

我有一个memoryStream实例,它已关闭.

我已经尝试过了:

memoryStream.Flush();
memoryStream.Position=0;
Run Code Online (Sandbox Code Playgroud)

要重新打开内存流但它不起作用.如何重新打开已关闭的内存流?

c#

35
推荐指数
4
解决办法
4万
查看次数

无法访问封闭流

我正在尝试使用缓存应用程序块来缓存一些图像(这些图像需要很长时间才能渲染)

  BitmapSource bitmapSource; ///some bitmap source already created
  _cache ///  Caching Application Block
  String someId; //id for this image, used as the key for the cache

  using (var stream = new MemoryStream())
    {
        PngBitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Interlace = PngInterlaceOption.On;
        encoder.Frames.Add(BitmapFrame.Create(bitmapSource));             
        encoder.Save(stream);
        _cache.Add(someId, stream);
    }
Run Code Online (Sandbox Code Playgroud)

然后使用以下方法加载它们

imStream = (Stream)_cache.GetData(someId));
if (imStream != null)
{
    PngBitmapDecoder decoder = new PngBitmapDecoder(imStream,  BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
    return decoder.Frames[0];  //return the bitmap source
}
Run Code Online (Sandbox Code Playgroud)

但在加载过程中,我在"新的PngBitmapDecoder"行中得到以下异常:

"无法访问封闭的流.

我理解我在上面的代码中关闭了流,但是在退出之前是不是_cache.Add()制作副本(通过序列化)?序列化流的正确过程是什么?

谢谢!

.net c# serialization caching-application-block

10
推荐指数
2
解决办法
2万
查看次数

为什么我在这里收到"无法访问封闭的流"?

堆栈跟踪看起来像

[ObjectDisposedException:无法访问已关闭的Stream.]
System.IO .__ Error.StreamIsClosed()+53
System.IO.MemoryStream.Read(Byte [] buffer,Int32 offset,Int32 count)+11411219 System.Web.Mvc.FileStreamResult. WriteFile(HttpResponseBase response)+81 System.Web.Mvc.FileResult.ExecuteResult(ControllerContext context)+168
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext,ActionResult actionResult)+13

在调用之后

        //Byte[] bytes;
        using ( var ms = new MemoryStream() )
        {
            using ( var doc = new Document() )
            {
                using ( var writer = PdfWriter.GetInstance(doc, ms) )
                {

                    doc.Open();
                    //var example_html = @"<p>This <em>is </em><span class=""headline"" style=""text-decoration: underline;"">some</span> <strong>sample <em> text</em></strong><span style=""color: red;"">!!!</span></p>";
                    var example_html = System.IO.File.ReadAllText(Path.Combine(Server.MapPath("~/EmailTemplates"), "template.html"));
                    var example_css = @".headline{font-size:200%}";
                    using ( var srHtml = new StringReader(example_html) ) …
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net asp.net-mvc itextsharp

7
推荐指数
2
解决办法
3万
查看次数

使用SendMessage通过电子邮件发送文件附件而不保存文件

我可以发送电子邮件,但我无法创建有效的附件()以放入我的电子邮件.我在网上找到的所有例子都假设它以某种方式保存在我的机器本地并通过路径链接它但事实并非如此.在我的方法中,我使用Winnovative创建文件,然后我想将它附加到电子邮件并发送它.不涉及储蓄.

protected void SendEmail_BtnClick(object sender, EventArgs a) 
{
    if(IsValidEmail(EmailTextBox.Text)) 
    {
        try
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.To.Add(EmailTextBox.Text);
            mailMessage.From = new MailAddress("my email");
            mailMessage.Subject = " Your Order";

            string htmlstring = GenerateHTMLString();

            Document pdfDocument = GeneratePDFReport(htmlstring);
            //Attachment attachment = new Attachment("Receipt.pdf",pdfDocument);
            //mailMessage.Attachments.Add();

            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
            //needs to change this password
            smtpClient.Credentials = new NetworkCredential("j@gmail.com","password"); //change password to password of email
            smtpClient.EnableSsl = true;
            try 
            {
                smtpClient.Send(mailMessage);
                EmailErrorMessage("Email Sent");
            }
            catch(Exception exc) 
            {
                EmailErrorMessage("Email could not be sent");
            } …
Run Code Online (Sandbox Code Playgroud)

c# asp.net email-attachments winnovative

2
推荐指数
1
解决办法
3444
查看次数