为什么 FileMode.Create 被另一个进程使用?
Why is the FileMode.Create being used in another process?
所以我目前正在开发一个简单的应用程序,它可以截取我的桌面屏幕截图,然后我可以通过电子邮件将其快速发送给朋友或家人。
事情是这样的..
当我按下 Inilizalize 按钮时,它 运行 通过这些方法..
screenshot();
setImage();
saveScreenshot();
sendScreenImage();
截取屏幕截图 > 将屏幕截图设置为图像容器中的来源 > 将内容保存在图像容器中 > 将其发送给我想要发送给的任何人。
简单吧?
一切都很好,假设我只是 运行 通过拍照并保存它,每次我按
截图时它可以替换(覆盖)旧的> 设置图像 > 保存屏幕截图
但是一旦我发送它(添加 sendScreenImage(); 方法)我就不能再覆盖旧图像因为它说正在使用该过程
Error message: The process cannot access the file because it is being used by another
process.
而且它总是在同一行的同一位置向我抛出该错误
using (FileStream stream = new FileStream(path, FileMode.Create))
在 saveScreenshot() 中;
--问题--
用什么进程?我是否需要对使用该进程的东西进行多线程处理,还是我可以取消它?
using System;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.Threading;
using System.Net.Mail;
using System.Net;
using System.IO;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Drawing;
using System.Windows.Media;
using System.Drawing.Imaging;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Interop;
using System.Windows.Threading;
using WPFCSharpWebCam;
namespace WebcamApplication
{
/// <summary>
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
#region methods
private void initializeToolBtn_Click(object sender, RoutedEventArgs e)
{
screenshot();
setImage();
saveScreenshot();
sendScreenImage();
}
private void screenshot()
{
SendKeys.SendWait("{PRTSC}");
}
private void setImage()
{
System.Windows.Clipboard.GetImage();
var clipboardImage = System.Windows.Clipboard.GetImage();
image.Source = clipboardImage;
}
private void saveScreenshot()
{
try
{
string path;
path = "%AppData%\image.png"; // collection of paths
path = Environment.ExpandEnvironmentVariables(path);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create((BitmapSource)image.Source));
using (FileStream stream = new FileStream(path, FileMode.Create))
encoder.Save(stream);
System.Windows.MessageBox.Show("Replaced the old screenshot");
return;
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(" saveScreenshot() went wrong " + ex.ToString());
}
}
private void sendScreenImage()
{
try
{
System.Windows.Clipboard.GetImage();
var clipboardImage = System.Windows.Clipboard.GetImage();
image.Source = clipboardImage;
string path;
path = "%AppData%\image.png"; // collection of paths
path = Environment.ExpandEnvironmentVariables(path);
MailMessage msg = new MailMessage();
msg.From = new MailAddress(emailfromTextbox.Text, "Screenshot Image");
msg.To.Add(new MailAddress(emailTextbox.Text));
msg.Subject = "SCreenshot Image";
msg.Body = clipboardImage.ToString();
msg.IsBodyHtml = true;
AlternateView plainView = AlternateView.CreateAlternateViewFromString("This is my plain text content, viewable by those clients that don't support html", null, "text/plain");
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("Here is an embedded image.<img src=cid:companylogo>", null, "text/html");
//create the LinkedResource(embedded image)
LinkedResource logo = new LinkedResource(path);
logo.ContentId = "image.png";
//add the LinkedResource to the appropriate view
htmlView.LinkedResources.Add(logo);
msg.AlternateViews.Add(plainView);
msg.AlternateViews.Add(htmlView);
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new NetworkCredential(emailTextbox.Text, emailpasswordTextbox.Text);
smtp.EnableSsl = true;
smtp.Send(msg);
System.Windows.MessageBox.Show("Done");
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.ToString(), "GMAIL Error" + ex.ToString());
}
}
LinkedResource
拥有读取附件的文件流。
您需要在发送电子邮件后在 using
块中处理它。
所以我目前正在开发一个简单的应用程序,它可以截取我的桌面屏幕截图,然后我可以通过电子邮件将其快速发送给朋友或家人。
事情是这样的.. 当我按下 Inilizalize 按钮时,它 运行 通过这些方法..
screenshot();
setImage();
saveScreenshot();
sendScreenImage();
截取屏幕截图 > 将屏幕截图设置为图像容器中的来源 > 将内容保存在图像容器中 > 将其发送给我想要发送给的任何人。
简单吧?
一切都很好,假设我只是 运行 通过拍照并保存它,每次我按
截图时它可以替换(覆盖)旧的> 设置图像 > 保存屏幕截图
但是一旦我发送它(添加 sendScreenImage(); 方法)我就不能再覆盖旧图像因为它说正在使用该过程
Error message: The process cannot access the file because it is being used by another process.
而且它总是在同一行的同一位置向我抛出该错误
using (FileStream stream = new FileStream(path, FileMode.Create))
在 saveScreenshot() 中;
--问题--
用什么进程?我是否需要对使用该进程的东西进行多线程处理,还是我可以取消它?
using System;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.Threading;
using System.Net.Mail;
using System.Net;
using System.IO;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Drawing;
using System.Windows.Media;
using System.Drawing.Imaging;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Interop;
using System.Windows.Threading;
using WPFCSharpWebCam;
namespace WebcamApplication
{
/// <summary>
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
#region methods
private void initializeToolBtn_Click(object sender, RoutedEventArgs e)
{
screenshot();
setImage();
saveScreenshot();
sendScreenImage();
}
private void screenshot()
{
SendKeys.SendWait("{PRTSC}");
}
private void setImage()
{
System.Windows.Clipboard.GetImage();
var clipboardImage = System.Windows.Clipboard.GetImage();
image.Source = clipboardImage;
}
private void saveScreenshot()
{
try
{
string path;
path = "%AppData%\image.png"; // collection of paths
path = Environment.ExpandEnvironmentVariables(path);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create((BitmapSource)image.Source));
using (FileStream stream = new FileStream(path, FileMode.Create))
encoder.Save(stream);
System.Windows.MessageBox.Show("Replaced the old screenshot");
return;
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(" saveScreenshot() went wrong " + ex.ToString());
}
}
private void sendScreenImage()
{
try
{
System.Windows.Clipboard.GetImage();
var clipboardImage = System.Windows.Clipboard.GetImage();
image.Source = clipboardImage;
string path;
path = "%AppData%\image.png"; // collection of paths
path = Environment.ExpandEnvironmentVariables(path);
MailMessage msg = new MailMessage();
msg.From = new MailAddress(emailfromTextbox.Text, "Screenshot Image");
msg.To.Add(new MailAddress(emailTextbox.Text));
msg.Subject = "SCreenshot Image";
msg.Body = clipboardImage.ToString();
msg.IsBodyHtml = true;
AlternateView plainView = AlternateView.CreateAlternateViewFromString("This is my plain text content, viewable by those clients that don't support html", null, "text/plain");
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("Here is an embedded image.<img src=cid:companylogo>", null, "text/html");
//create the LinkedResource(embedded image)
LinkedResource logo = new LinkedResource(path);
logo.ContentId = "image.png";
//add the LinkedResource to the appropriate view
htmlView.LinkedResources.Add(logo);
msg.AlternateViews.Add(plainView);
msg.AlternateViews.Add(htmlView);
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new NetworkCredential(emailTextbox.Text, emailpasswordTextbox.Text);
smtp.EnableSsl = true;
smtp.Send(msg);
System.Windows.MessageBox.Show("Done");
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.ToString(), "GMAIL Error" + ex.ToString());
}
}
LinkedResource
拥有读取附件的文件流。
您需要在发送电子邮件后在 using
块中处理它。