질문 :내가 읽은 XML 파일을 덮어 쓸 수없는 이유는 무엇입니까 (모든 스트림을 닫은 것 같습니다).
아래의 프로그램을 복사 한 후, strPat
에 지정된 폴더에있는 모든 XML 보고서 파일 (* .rdl 파일)를 읽고 수정할 내 코드에 따라, 그리고 폴더 <User>/Desktop/AutoModifiedReports/filename.rdl
에서 수정 된 XML 파일을 저장한다 수정 된 XML 파일을 strPat
의 폴더로 다시 가져옵니다.
내 문제는 카피 백 절차에서 승인되지 않은 액세스 예외가 발생한다는 것입니다. 프로그램은 대상 디렉토리에 파일을 쓰고 덮어 쓸 수 있습니다. 처음에 읽고 수정 한 다음 다시 복사하면 무단 액세스 예외가 발생합니다.
는이
public static void CopyBack(string strFileName)
{
string strSavePath = GetSavePath();
strSavePath = System.IO.Path.Combine(strSavePath, System.IO.Path.GetFileName(strFileName));
if (System.IO.File.Exists(strSavePath))
{
System.IO.File.Copy(strSavePath, strFileName, true); // Exception - Unauthorized access
}
} // End Sub CopyBack
에서 나는 열린 스트림을 떠나 어떤 장소 잠긴 파일이 표시되지 않는 발생합니다.
누구나 내가 잘못하고 있다는 것을 정확히 말해 줄 수 있습니까? 어떻게 해결할 수 있습니까?
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace ReportModifier
{
static class MalfunctioningProgram
{
public static string strLogPath = GetLogFilePath();
public static string GetSavePath()
{
string strSavePath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
strSavePath = System.IO.Path.Combine(strSavePath, "AutoModifiedReports");
if (!System.IO.Directory.Exists(strSavePath))
System.IO.Directory.CreateDirectory(strSavePath);
return strSavePath;
} // End Function GetSavePath
public static string GetLogFilePath()
{
string strLogfileLocation = System.IO.Path.Combine(GetSavePath(), "log.txt");
if (System.IO.File.Exists(strLogfileLocation))
System.IO.File.Delete(strLogfileLocation);
return strLogfileLocation;
} // End Function GetLogFilePath
public static void LogMessage(string strMessage)
{
// Console.WriteLine(strMessage);
System.IO.File.AppendAllText(strLogPath, strMessage + Environment.NewLine, System.Text.Encoding.UTF8);
} // End Sub LogMessage
public static void LogMessage(string str, params object[] args)
{
LogMessage(string.Format(str, args));
} // End Sub LogMessage
public static System.Xml.XmlDocument File2XmlDocument(string strFileName)
{
// http://blogs.msdn.com/b/tolong/archive/2007/11/15/read-write-xml-in-memory-stream.aspx
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
// doc.Load(memorystream);
// doc.Load(FILE_NAME);
using (System.Xml.XmlTextReader xtrReader = new System.Xml.XmlTextReader(strFileName))
{
doc.Load(xtrReader);
xtrReader.Close();
} // End Using xtrReader
return doc;
} // End Function File2XmlDocument
public static System.Xml.XmlNamespaceManager GetReportNamespaceManager(System.Xml.XmlDocument doc)
{
System.Xml.XmlNamespaceManager nsmgr = new System.Xml.XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("dft", "http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition");
return nsmgr;
} // End Function GetReportNamespaceManager
public static void SaveDocument(System.Xml.XmlDocument doc, string strFilename)
{
SaveDocument(doc, strFilename, false);
} // End Sub SaveDocument
public static void SaveDocument(System.Xml.XmlDocument doc, string strFilename, bool bDoReplace)
{
string strSavePath = GetSavePath();
strSavePath = System.IO.Path.Combine(strSavePath, System.IO.Path.GetFileName(strFilename));
if (bDoReplace)
{
doc.LoadXml(doc.OuterXml.Replace("xmlns=\"\"", ""));
}
using (System.Xml.XmlTextWriter xtw = new System.Xml.XmlTextWriter(strSavePath, System.Text.Encoding.UTF8))
{
xtw.Formatting = System.Xml.Formatting.Indented; // if you want it indented
xtw.Indentation = 4;
xtw.IndentChar = ' ';
doc.Save(xtw);
xtw.Flush();
xtw.Close();
} // End Using xtw
doc = null;
} // End Sub SaveDocument
public static void ChangeParameterPrompt(string strFilename, string strReportParameterName, string strReplacementText)
{
System.Xml.XmlDocument doc = File2XmlDocument(strFilename);
System.Xml.XmlNamespaceManager nsmgr = GetReportNamespaceManager(doc);
if (!HasParameter(doc, strReportParameterName))
return;
System.Xml.XmlNode xnParameterPrompt = GetParameterPrompt(doc, strReportParameterName);
string strReportName = System.IO.Path.GetFileNameWithoutExtension(strFilename);
if (xnParameterPrompt != null)
{
string strParameterValue = xnParameterPrompt.FirstChild.Value;
xnParameterPrompt.FirstChild.Value = strReplacementText;
LogMessage("Old value in {0}:\t{1}", strReportName, strParameterValue);
}
else
LogMessage("{0}\tKein Parameter " + strReportParameterName, strReportName);
SaveDocument(doc, strFilename);
} // End Sub ChangeParameterPrompt
public static void ChangeStichtag(string strFilename)
{
System.Xml.XmlDocument doc = File2XmlDocument(strFilename);
System.Xml.XmlNamespaceManager nsmgr = GetReportNamespaceManager(doc);
System.Xml.XmlNode xnStichtag = doc.SelectSingleNode("somequery", nsmgr);
string strReportName = System.IO.Path.GetFileNameWithoutExtension(strFilename);
if (!HasParameter(doc, "in_stichtag"))
return;
if (xnStichtag != null)
{
xnStichtag.FirstChild.Value = "=System.DateTime.Now.ToString(\"dd.MM.yyyy\")";
string strStichTag = xnStichtag.FirstChild.Value;
LogMessage("{0}\t{1}", strReportName, strStichTag);
}
else
LogMessage("{0}\tKein Parameter Stichtag", strReportName);
SaveDocument(doc, strFilename);
} // End Sub ChangeStichtag
public static string XmlEscape(string unescaped)
{
string strReturnValue = null;
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
System.Xml.XmlNode node = doc.CreateElement("root");
node.InnerText = unescaped;
strReturnValue = node.InnerXml;
node = null;
doc = null;
return strReturnValue;
} // End Function XmlEscape
public static string XmlUnescape(string escaped)
{
string strReturnValue = null;
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
System.Xml.XmlNode node = doc.CreateElement("root");
node.InnerXml = escaped;
strReturnValue = node.InnerText;
node = null;
doc = null;
return strReturnValue;
} // End Function XmlUnescape
public static bool HasParameter(System.Xml.XmlDocument doc, string strParameterName)
{
strParameterName = XmlEscape(strParameterName);
System.Xml.XmlNamespaceManager nsmgr = GetReportNamespaceManager(doc);
System.Xml.XmlNode xnProc = doc.SelectSingleNode("somequery", nsmgr);
return xnProc != null;
} // End Function HasParameter
public static System.Xml.XmlNode GetParameter(System.Xml.XmlDocument doc, string strParameterName)
{
strParameterName = XmlEscape(strParameterName);
System.Xml.XmlNamespaceManager nsmgr = GetReportNamespaceManager(doc);
System.Xml.XmlNode xnParam = doc.SelectSingleNode("somequery", nsmgr);
return xnParam;
} // End Function GetParameter
public static System.Xml.XmlNode GetParameterPrompt(System.Xml.XmlDocument doc, string strParameterName)
{
strParameterName = XmlEscape(strParameterName);
System.Xml.XmlNamespaceManager nsmgr = GetReportNamespaceManager(doc);
System.Xml.XmlNode xnParam = doc.SelectSingleNode("somequery", nsmgr);
return xnParam;
} // End Function GetParameter
public static void AddProc(string strFilename)
{
System.Xml.XmlDocument doc = File2XmlDocument(strFilename);
System.Xml.XmlNamespaceManager nsmgr = GetReportNamespaceManager(doc);
if (HasParameter(doc, "proc"))
return;
System.Xml.XmlNode xnMandant = GetParameter(doc, "in_mandant");
string strReportName = System.IO.Path.GetFileNameWithoutExtension(strFilename);
if (xnMandant != null)
{
LogMessage("{0}\t{1}", strReportName, xnMandant.FirstChild.Value);
string frag = @"some xml fragment";
System.Xml.XmlDocumentFragment xmlDocFrag = doc.CreateDocumentFragment();
xmlDocFrag.InnerXml = frag;
// System.Xml.XmlNode xn =
xnMandant.ParentNode.InsertAfter(xmlDocFrag, xnMandant);
}
else
LogMessage("{0}\tKein Parameter in_mandant", strReportName);
SaveDocument(doc, strFilename, true);
//SaveDocument(doc, strFilename, "<ReportParameter Name=\"proc\" xmlns=\"\">", "<ReportParameter Name=\"proc\">");
} // End Sub AddProc
public static List<string> GetAllReports(string strPath)
{
List<string> ls = new List<string>();
ls.AddRange(System.IO.Directory.GetFiles(strPath, "*.rdl"));
return ls;
} // End Function GetAllReports
public static void CopyToSaveDirectory(string strFileName)
{
string strSavePath = GetSavePath();
strSavePath = System.IO.Path.Combine(strSavePath, System.IO.Path.GetFileName(strFileName));
System.IO.File.Copy(strFileName, strSavePath, true);
} // End Sub CopyToSaveDirectory
public static void CopyBack(string strFileName)
{
string strSavePath = GetSavePath();
strSavePath = System.IO.Path.Combine(strSavePath, System.IO.Path.GetFileName(strFileName));
if (System.IO.File.Exists(strSavePath))
{
System.IO.File.Copy(strSavePath, strFileName, true); // Exception - Unauthorized access
}
} // End Sub CopyBack
public static void AlterReports(string strPath)
{
List<string> lsReports = GetAllReports(strPath);
foreach (string strFileName in lsReports)
{
ChangeStichtag(strFileName);
CopyBack(strFileName);
ChangeParameterPrompt(strFileName, "in_standort", "Liegenschaft/Immeuble/Patrimonio immobiliare/Estate");
CopyBack(strFileName);
ChangeParameterPrompt(strFileName, "in_gebaeude", "Gebäude/Bâtiment/Edificio/Building");
CopyBack(strFileName);
ChangeParameterPrompt(strFileName, "in_geschoss", "Geschoss/Étage/Piano/Floor");
CopyBack(strFileName);
ChangeParameterPrompt(strFileName, "in_trakt", "Trakt/Aile/Ala/Wing");
CopyBack(strFileName);
ChangeParameterPrompt(strFileName, "in_haus", "Haus/Maison/Casa/House");
CopyBack(strFileName);
ChangeParameterPrompt(strFileName, "in_raum", "Raum/Pièce/Stanza/Room");
CopyBack(strFileName);
ChangeParameterPrompt(strFileName, "in_stichtag", "Stichtag/Jour de référence/Giorno di riferimento/Reporting date");
CopyBack(strFileName);
ChangeParameterPrompt(strFileName, "in_mietertrag", "Mindestertrag Mindestertrag/Rendement minimum/Rendimento minimo/Minimum yield");
CopyBack(strFileName);
} // Next strFileName
} // End Sub InvestigateStichtag
/// <summary>
/// Der Haupteinstiegspunkt für die Anwendung.
/// </summary>
[STAThread]
static void Main()
{
string strPath = @"S:\SomeBody\SomeFolder\Reports";
AlterReports(strPath);
Console.WriteLine(Environment.NewLine);
Console.WriteLine(" --- Press any key to continue --- ");
Console.ReadKey();
} // End Sub Main
} // End Class Program
} // End Namespace ReportModifier
관련성없는 코드를 삭제하십시오. 관련 코드 만 게시하십시오. –
@Sriram Sakthivel : 이미 해 봤습니다. 어떤 관련없는 비트가 보이나요? –
CopyBack을 연속 9 회 호출하면 각 보고서 뷰어 파일에 대해이 오류가 발생할 때를 지정할 수 있습니까? – JMK