0
這是一個跟進到this SO post,我成功地從一個WinForms應用程序在AML上實現AES加密以從Windows服務解密它。這僅僅是一個像c:\ test.xml這樣的文件可用於這兩個程序。否則,我從Windows服務這種可怕的例外:這個C#/ XML代碼鎖定文件/導致訪問被拒絕到另一個進程?
Unable to load configuration data. Access to the path 'C:\worl\Project Alpha\Code\AlphaConfigurationUtility\AlphaConfigurationUtility\bin\Debug\alphaService.xml' is denied.
很明顯,硬編碼不會在生產,但今天的工作,我把在WinForm和Windows服務項目都,並把它們寫和從同一目錄中讀取。現在我又得到了同樣可怕的異常。
編輯:可以有一些Windows服務的權限問題?它使用管理員帳戶運行
此代碼中的哪些內容可能導致文件被鎖定以便從其他程序訪問Windows服務程序?
try
{
string fileName = System.IO.Path.Combine(Application.StartupPath, "alphaService.xml");
// var fileName = @"c:/text.xml";
XDocument doc = new XDocument();
XElement xml = new XElement("Info",
new XElement("DatabaseServerName", txtServerName.Text),
new XElement("DatabaseUserName", txtDatabaseUserName.Text),
new XElement("DatabasePassword", txtDatabasePassword.Text),
new XElement("ServiceAccount", txtAccount.Text),
new XElement("ServicePassword", txtServicePassword.Text),
new XElement("RegistrationCode", txtRegistrationCode.Text));
doc.Add(xml);
//using (var aes = Aes.Create())
//{
// aesKey = aes.Key;
// key = Convert.ToBase64String(aes.Key);
//}
string sKey = "LvtZELDrB394hbSOi3SurLWAvC8adNpZiJmQDJHdfJU=";
var aesKey = Convert.FromBase64String(sKey);
string encyptedText = EncryptDecrpt.EncryptStringToBase64String(doc.ToString(), aesKey);
File.WriteAllText(fileName, encyptedText);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
的解密程序,Windows服務與啓動,並得到在File.ReadAllText例外:
string path = AppDomain.CurrentDomain.BaseDirectory;
eventLog1.WriteEntry(path);
string fileName = System.IO.Path.Combine(path, "alphaService.xml");
// var fileName = @"c:/text.xml";
string sKey = "LvtZELDrB394hbSOi3SurLWAvC8adNpZiJmQDJHdfJU=";
Byte[] keyBytes = Convert.FromBase64String(sKey);
var encryptedText = File.ReadAllText(fileName, new ASCIIEncoding());
謝謝你的建議,但會產生相同的例外。 – user2471435
嗯,你可以從服務程序訪問該目錄中的任何其他文件嗎? – silent