我一直在保存一個小的XML數據文件到外部驅動器,沒有probs。但後來我試圖使用ApplicationData文件夾和其他人,甚至C:\但沒有運氣。我遇到了「訪問路徑」C:\「拒絕」之類的錯誤。「訪問路徑...被拒絕」(.NET C#)
只是爲了確認,文件是用當前代碼創建並讀取到外部驅動器。我想這是與安全&權限有關,但我沒有發現任何有用的東西。
在此先感謝您,如果您能指出我在這個正確的方向!
string fipData = @"F:\IL2\SIIYM\SIIYM Data.xml"; // external drive ok :-)
//string fipData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
//string fipData = @"C:\";
// if the XML data file doesn't exist, create it
bool dataFileExists = File.Exists(fipData);
if (dataFileExists)
{
// read the XML values
XDocument xData = XDocument.Load(fipData);
//...
}
else
{
// create & save the XML data file
XElement xLastPath = new XElement(el_lastPath, "");
XElement xLastCode = new XElement(el_lastCode, "");
XElement xRoot = new XElement(el_root);
xRoot.Add(xLastPath);
xRoot.Add(xLastCode);
XDocument newDataFile = new XDocument();
newDataFile.Add(xRoot);
try
{
newDataFile.Save(fipData);
}
catch (Exception ex)
{
MessageBox.Show("Data file unable to be created. System message:{0}".Put(Environment.NewLine + Environment.NewLine + ex.Message));
}
}
嗯...我不知道爲什麼,但今天早上醒來,它適用於ApplicationData。很高興知道爲什麼。從Debug到Release構建時,我遇到了一些問題。我只能假設它與此有關。謝謝你的想法! – MrGreggles 2009-09-21 02:18:38
示例代碼中註釋掉的「fipData」聲明實際上並未指定文件名 - 只是路徑。這是一個錯字嗎? – 2009-09-22 05:08:05
腮紅馬特,這可能是原因。漫長的一天,腦力激盪。謝謝你們所有的想法! – MrGreggles 2009-09-26 07:06:16