HI们创建一个XML文件并保存它,但是说另一个进程正在使用该文件,我怎么能杀死它? [英] HI guys creating an XML file and saving it, but says file is in use by another process, how can I kill that?

查看:65
本文介绍了HI们创建一个XML文件并保存它,但是说另一个进程正在使用该文件,我怎么能杀死它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是xml文件中的新手,所以我希望有人能说出我做错了什么以及出了什么问题。



我的C#

 XmlDocument doc =  new  XmlDocument(); 
XmlNode docNode = doc.CreateXmlDeclaration( 1.0 UTF-8 null );
doc.AppendChild(docNode);



XmlNode requestLoginNode = doc.CreateElement( DriverAccept );
doc.AppendChild(requestLoginNode);

XmlNode RecordNode = doc.CreateElement( RecordType);
RecordNode.AppendChild(doc.CreateTextNode( DriverAccept));
requestLoginNode.AppendChild(RecordNode);

XmlNode comNode = doc.CreateElement( WebPrefix);
comNode.AppendChild(doc.CreateTextNode(prefix.ToString()));
requestLoginNode.AppendChild(comNode);

TextBox text = e.Item.FindControl( TourID)< span class =code-keyword> as
TextBox;

XmlNode tourIDNode = doc.CreateElement( TourID);
tourIDNode.AppendChild(doc.CreateTextNode(text.Text));
requestLoginNode.AppendChild(tourIDNode);

XmlNode drivNode = doc.CreateElement( UserName);
drivNode.AppendChild(doc.CreateTextNode(user.ToString()));
requestLoginNode.AppendChild(drivNode);




doc.Save(Server.MapPath( ../ DriverAccept / + Request.QueryString [ WebPrefix] + Request.QueryString [ UserName] + DriverAccept.xml));



 尝试 
{
string postData = doc.OuterXml.ToString();
byte [] byteArray = Encoding.UTF8.GetBytes(postData);
// 设置WebRequest的ContentType属性。
request.ContentType = application / xml;


// 设置WebRequest的ContentLength属性。
request.ContentLength = byteArray.Length;
// 获取请求流。
流dataStream = request.GetRequestStream( );
// 将数据写入请求流。
dataStream.Write( byteArray, 0 ,byteArray.Length);
// 关闭Stream对象。
dataStream.Close();
// 获取回复。
WebResponse response = request.GetResponse() ;
// 显示状态。
Console.WriteLine(((HttpWebResponse)响应).StatusDescription);
// 获取包含服务器返回内容的流。
dataStream = response.GetResponseStream();
// 使用StreamReader打开流以便于访问。
StreamReader阅读器= new StreamReader(dataStream);
// 阅读内容。
string responseFromServer = reader.ReadToEnd();
// 显示内容。
string filename = Server.MapPath( ../ DriverAccept / + Request .QueryString [ WebPrefix] + Request.QueryString [ UserName] + DriverAcceptResponse。 xml的);



TextWriter tw = new StreamWriter(filename);
tw.Write(responseFromServer.Substring( 0 ));
tw.Close();

// 清理流。
reader.Close ();
dataStream.Close();
response.Close();


}
catch (WebException ex)
{


string Subject = DriverAccept Request fejlede
Body = Serverfejl + + urlPath +
ToEmail = mail;
string SMTPUser = mymail,SMTPPassword = ******;

// 现在实例化MailMessage的新实例
MailMessage mail = new MailMessage();

// 设置邮件的发件人地址
mail.From = new MailAddress(SMTPUser,Request.QueryString [ WebPrefix]。ToString()+ );

// 设置邮件的接收地址
mail.To.Add(ToEmail);

// 设置邮件主题
mail .Subject =主题;

// 设置邮件正文
mail .Body =身体;

// 即使你没有发送HTML消息也要保持原样
mail.IsBodyHtml = true ;



/// /添加我们的附件
// 附件AttXml =新附件(Server.MapPath(../ DriverAccept /+ Request) .QueryString [WebPrefix] + Request.QueryString [UserName] +DriverAccept.xml));
/// /给它一个内容ID,对应于我们在body img标签中添加的src
// AttXml.ContentId =DriverAccept / DriverAccept.xml;



/// /将附件添加到电子邮件
// mail.Attachments.Add(AttXml);
// 将邮件的优先级设置为正常
mail.Priority = MailPriority.Normal;

// 实例化SmtpClient的新实例
SmtpClient smtp = new SmtpClient();

// 如果您使用的是smtp服务器,请将主机更改为smtp。 yourdomain.com
smtp.Host = smtp.gmail.com;

// 为您的主机输入您的端口
smtp。端口= 25 ; // 或者您也可以使用#587端口

// 提供smtp凭据以对您的帐户进行身份验证
smtp.Credentials = new System.Net.NetworkCredential(SMTPUser,SMTPPassword);

// 如果您使用SSL / TLS使用安全身份验证,则trueelse false
smtp.EnableSsl = true ;

smtp.Send(mail);


}

requestList();


}





  public   void  requestList()
{

string prefix = Request.QueryString [ WebPrefix];

string user = Request.QueryString [ 用户名];

XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration( 1.0 UTF-8 null );
doc.AppendChild(docNode);

XmlNode requestLoginNode = doc.CreateElement( DriverListRequest);
doc.AppendChild(requestLoginNode);

XmlNode RecordNode = doc.CreateElement( RecordType);
RecordNode.AppendChild(doc.CreateTextNode( DriverList));
requestLoginNode.AppendChild(RecordNode);

XmlNode comNode = doc.CreateElement( WebPrefix);
comNode.AppendChild(doc.CreateTextNode(prefix.ToString()));
requestLoginNode.AppendChild(comNode);

XmlNode drivNode = doc.CreateElement( UserName);
drivNode.AppendChild(doc.CreateTextNode(user.ToString()));
requestLoginNode.AppendChild(drivNode);



doc.Save(Server.MapPath( 。 ./xmlFiler / + Request.QueryString [ WebPrefix] + Request.QueryString [ UserName] + < span class =code-string> DriverListRequest.xml
));







尝试
{
string postData = doc.OuterXml.ToString();
byte [] byteArray = Encoding.UTF8.GetBytes(postData);
// 设置WebRequest的ContentType属性。
request.ContentType = application / xml;


// 设置WebRequest的ContentLength属性。
request.ContentLength = byteArray.Length;
// 获取请求流。
流dataStream = request.GetRequestStream( );
// 将数据写入请求流。
dataStream.Write( byteArray, 0 ,byteArray.Length);
// 关闭Stream对象。
dataStream.Close();
// 获取回复。
WebResponse response = request.GetResponse() ;
// 显示状态。
Console.WriteLine(((HttpWebResponse)响应).StatusDescription);
// 获取包含服务器返回内容的流。
dataStream = response.GetResponseStream();
// 使用StreamReader打开流以便于访问。
StreamReader阅读器= new StreamReader(dataStream);
// 阅读内容。
string responseFromServer = reader.ReadToEnd();
// 显示内容。
string filename = Server.MapPath( ../ xmlReDriverListResponse / + Request .QueryString [ WebPrefix] + Request.QueryString [ UserName] + DriverListResponse。 xml的);





TextWriter tw = new StreamWriter(filename);
tw.Write(responseFromServer.Substring( 0 ));
tw.Close();

// 清理流。
reader.Close ();
dataStream.Close();
response.Close();


}
catch (WebException ex)
{


string Subject = DriverList Request fejlede
Body = Serverfejl + + urlPath +
ToEmail = mail;
string SMTPUser = mail,SMTPPassword = *******;

// 现在实例化MailMessage的新实例
MailMessage mail = new MailMessage();

// 设置邮件的发件人地址
mail.From = new MailAddress(SMTPUser,Request.QueryString [ WebPrefix]。ToString()+ );

// 设置邮件的接收地址
mail.To.Add(ToEmail);

// 设置邮件主题
mail .Subject =主题;

// 设置邮件正文
mail .Body =身体;

// 即使你没有发送HTML消息也要保持原样
mail.IsBodyHtml = true ;



// 添加我们的附件
附件AttXml = new 附件(Server.MapPath( ../ xmlReDriverListResponse / + Request.QueryString [ WebPrefix] + Request.QueryString [ UserName] + DriverListResponse.xml));
// 给它一个内容ID,对应于我们在body img标签中添加的src
AttXml.ContentId = xmlReDriverListResponse / DriverListResponse.xml;



// 将附件添加到电子邮件中
mail.Attachments.Add(AttXml);
// 将邮件的优先级设置为正常
mail.Priority = MailPriority.Normal;

// 实例化SmtpClient的新实例
SmtpClient smtp = new SmtpClient();

// 如果您使用的是smtp服务器,请将主机更改为smtp。 yourdomain.com
smtp.Host = smtp.gmail.com;

// 为您的主机输入您的端口
smtp。端口= 25 ; // 或者您也可以使用#587端口

// 提供smtp凭据以对您的帐户进行身份验证
smtp.Credentials = new System.Net.NetworkCredential(SMTPUser,SMTPPassword);

// 如果您使用SSL / TLS使用安全身份验证,则trueelse false
smtp.EnableSsl = true ;

smtp.Send(mail);
}

string path1 = Server.MapPath( < span class =code-string> ../ xmlReDriverListResponse /
+ Request.QueryString [ WebPrefix ] + Request.QueryString [ UserName] + DriverListResponse.xml);

XmlDocument doc2 = new XmlDocument();
doc2.Load(path1);

XmlNodeList lst = doc2.SelectNodes( // Recordtype);


for int i = 0 ; i < lst.Count; i ++)
{
XmlNode node = lst [i] ;
XmlAttribute att = doc2.CreateAttribute( record);


att.InnerText =(i + 1 )。ToString();
node.Attributes.Append(att);


XmlNode t = doc2.SelectSingleNode( / DriverListResponse / Recordtype [@记录= '1']);
t.ParentNode.RemoveChild(t);


doc2.Save(path1);
}


}





我的尝试:



Google和rewritte .....和xmlwritter

解决方案

应用程序应该永远不会杀死任何其他进程。



当一个文件被另一个进程使用时显示相应的错误消息。然后,用户可以识别该进程以关闭该文件。 (可选)提供重试按钮和/或更改文件名的选项。



要获得打开文件的进程,可以使用过程资源管理器 [ ^ ]。


为什么不使用

 XmlDocument.Save ()


I am a rookie in xml files, so i hope someone can tell what i am doing wrong and where is going wrong.

my C#

XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);



XmlNode requestLoginNode = doc.CreateElement("DriverAccept");
doc.AppendChild(requestLoginNode);

XmlNode RecordNode = doc.CreateElement("RecordType");
RecordNode.AppendChild(doc.CreateTextNode("DriverAccept"));
requestLoginNode.AppendChild(RecordNode);

XmlNode comNode = doc.CreateElement("WebPrefix");
comNode.AppendChild(doc.CreateTextNode(prefix.ToString()));
requestLoginNode.AppendChild(comNode);

TextBox text = e.Item.FindControl("TourID") as TextBox;

XmlNode tourIDNode = doc.CreateElement("TourID");
tourIDNode.AppendChild(doc.CreateTextNode(text.Text));
requestLoginNode.AppendChild(tourIDNode);

XmlNode drivNode = doc.CreateElement("UserName");
drivNode.AppendChild(doc.CreateTextNode(user.ToString()));
requestLoginNode.AppendChild(drivNode);




doc.Save(Server.MapPath("../DriverAccept/" + Request.QueryString["WebPrefix"] + Request.QueryString["UserName"] + "DriverAccept.xml"));


    try
    {
        string postData = doc.OuterXml.ToString();
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/xml";


        // Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length;
        // Get the request stream.
        Stream dataStream = request.GetRequestStream();
        // Write the data to the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length);
        // Close the Stream object.
        dataStream.Close();
        // Get the response.
        WebResponse response = request.GetResponse();
        // Display the status.
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        // Display the content.
        string filename = Server.MapPath("../DriverAccept/" + Request.QueryString["WebPrefix"] + Request.QueryString["UserName"] + "DriverAcceptResponse.xml");



        TextWriter tw = new StreamWriter(filename);
        tw.Write(responseFromServer.Substring(0));
        tw.Close();

        // Clean up the streams.
        reader.Close();
        dataStream.Close();
        response.Close();


    }
    catch (WebException ex)
    {


        string Subject = "DriverAccept Request fejlede",
        Body = "Serverfejl" + " " + urlPath + "",
        ToEmail = "mail";
        string SMTPUser = "mymail", SMTPPassword = "******";

        //Now instantiate a new instance of MailMessage
        MailMessage mail = new MailMessage();

        //set the sender address of the mail message
        mail.From = new MailAddress(SMTPUser, Request.QueryString["WebPrefix"].ToString() + " ");

        //set the recepient addresses of the mail message
        mail.To.Add(ToEmail);

        //set the subject of the mail message
        mail.Subject = Subject;

        //set the body of the mail message
        mail.Body = Body;

        //leave as it is even if you are not sending HTML message
        mail.IsBodyHtml = true;



        ////add our attachment
        //Attachment AttXml = new Attachment(Server.MapPath("../DriverAccept/" + Request.QueryString["WebPrefix"] + Request.QueryString["UserName"] + "DriverAccept.xml"));
        ////give it a content id that corresponds to the src we added in the body img tag
        //AttXml.ContentId = "DriverAccept/DriverAccept.xml";



        ////add the attachment to the email
        //mail.Attachments.Add(AttXml);
        //set the priority of the mail message to normal
        mail.Priority = MailPriority.Normal;

        //instantiate a new instance of SmtpClient
        SmtpClient smtp = new SmtpClient();

        //if you are using your smtp server, then change your host like "smtp.yourdomain.com"
        smtp.Host = "smtp.gmail.com";

        //chnage your port for your host
        smtp.Port = 25; //or you can also use port# 587

        //provide smtp credentials to authenticate to your account
        smtp.Credentials = new System.Net.NetworkCredential(SMTPUser, SMTPPassword);

        //if you are using secure authentication using SSL/TLS then "true" else "false"
        smtp.EnableSsl = true;

        smtp.Send(mail);


    }

    requestList();


}



public void requestList()
   {

       string prefix = Request.QueryString["WebPrefix"];

       string user = Request.QueryString["UserName"];

       XmlDocument doc = new XmlDocument();
       XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
       doc.AppendChild(docNode);

       XmlNode requestLoginNode = doc.CreateElement("DriverListRequest");
       doc.AppendChild(requestLoginNode);

       XmlNode RecordNode = doc.CreateElement("RecordType");
       RecordNode.AppendChild(doc.CreateTextNode("DriverList"));
       requestLoginNode.AppendChild(RecordNode);

       XmlNode comNode = doc.CreateElement("WebPrefix");
       comNode.AppendChild(doc.CreateTextNode(prefix.ToString()));
       requestLoginNode.AppendChild(comNode);

       XmlNode drivNode = doc.CreateElement("UserName");
       drivNode.AppendChild(doc.CreateTextNode(user.ToString()));
       requestLoginNode.AppendChild(drivNode);



       doc.Save(Server.MapPath("../xmlFiler/" + Request.QueryString["WebPrefix"] + Request.QueryString["UserName"] + "DriverListRequest.xml"));







       try
       {
           string postData = doc.OuterXml.ToString();
           byte[] byteArray = Encoding.UTF8.GetBytes(postData);
           // Set the ContentType property of the WebRequest.
           request.ContentType = "application/xml";


           // Set the ContentLength property of the WebRequest.
           request.ContentLength = byteArray.Length;
           // Get the request stream.
           Stream dataStream = request.GetRequestStream();
           // Write the data to the request stream.
           dataStream.Write(byteArray, 0, byteArray.Length);
           // Close the Stream object.
           dataStream.Close();
           // Get the response.
           WebResponse response = request.GetResponse();
           // Display the status.
           Console.WriteLine(((HttpWebResponse)response).StatusDescription);
           // Get the stream containing content returned by the server.
           dataStream = response.GetResponseStream();
           // Open the stream using a StreamReader for easy access.
           StreamReader reader = new StreamReader(dataStream);
           // Read the content.
           string responseFromServer = reader.ReadToEnd();
           // Display the content.
           string filename = Server.MapPath("../xmlReDriverListResponse/" + Request.QueryString["WebPrefix"] + Request.QueryString["UserName"] + "DriverListResponse.xml");





           TextWriter tw = new StreamWriter(filename);
           tw.Write(responseFromServer.Substring(0));
           tw.Close();

           // Clean up the streams.
           reader.Close();
           dataStream.Close();
           response.Close();


       }
       catch (WebException ex)
       {


           string Subject = "DriverList Request fejlede",
           Body = "Serverfejl" + " " + urlPath + "",
           ToEmail = "mail";
           string SMTPUser = "mail", SMTPPassword = "*******";

           //Now instantiate a new instance of MailMessage
           MailMessage mail = new MailMessage();

           //set the sender address of the mail message
           mail.From = new MailAddress(SMTPUser, Request.QueryString["WebPrefix"].ToString() + " ");

           //set the recepient addresses of the mail message
           mail.To.Add(ToEmail);

           //set the subject of the mail message
           mail.Subject = Subject;

           //set the body of the mail message
           mail.Body = Body;

           //leave as it is even if you are not sending HTML message
           mail.IsBodyHtml = true;



           //add our attachment
           Attachment AttXml = new Attachment(Server.MapPath("../xmlReDriverListResponse/" + Request.QueryString["WebPrefix"] + Request.QueryString["UserName"] + "DriverListResponse.xml"));
           //give it a content id that corresponds to the src we added in the body img tag
           AttXml.ContentId = "xmlReDriverListResponse/DriverListResponse.xml";



           //add the attachment to the email
           mail.Attachments.Add(AttXml);
           //set the priority of the mail message to normal
           mail.Priority = MailPriority.Normal;

           //instantiate a new instance of SmtpClient
           SmtpClient smtp = new SmtpClient();

           //if you are using your smtp server, then change your host like "smtp.yourdomain.com"
           smtp.Host = "smtp.gmail.com";

           //chnage your port for your host
           smtp.Port = 25; //or you can also use port# 587

           //provide smtp credentials to authenticate to your account
           smtp.Credentials = new System.Net.NetworkCredential(SMTPUser, SMTPPassword);

           //if you are using secure authentication using SSL/TLS then "true" else "false"
           smtp.EnableSsl = true;

           smtp.Send(mail);
       }

       string path1 = Server.MapPath("../xmlReDriverListResponse/" + Request.QueryString["WebPrefix"] + Request.QueryString["UserName"] + "DriverListResponse.xml");

       XmlDocument doc2 = new XmlDocument();
       doc2.Load(path1);

       XmlNodeList lst = doc2.SelectNodes("//Recordtype");


       for (int i = 0; i < lst.Count; i++)
       {
           XmlNode node = lst[i];
           XmlAttribute att = doc2.CreateAttribute("record");


           att.InnerText = (i + 1).ToString();
           node.Attributes.Append(att);


           XmlNode t = doc2.SelectSingleNode("/DriverListResponse/Recordtype[@record='1']");
           t.ParentNode.RemoveChild(t);


           doc2.Save(path1);
       }


   }



What I have tried:

Google and rewritte..... and xmlwritter

解决方案

An application should never kill any other process.

When a file is in use by another process show a corresponding error message. The user might then identify that process to close the file there. Optionally provide a Retry button and / or an option to change the file name.

To get the process that has opened the file you can use the Process Explorer[^].


Why don't you use

XmlDocument.Save()

?


这篇关于HI们创建一个XML文件并保存它,但是说另一个进程正在使用该文件,我怎么能杀死它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆