[英]Access to the path is denied when save with filedialog
我有兩種保存到文件的形式:
一。 我將路徑保留在代碼中。
二。 我從用戶那里得到路徑。
當我在代碼中保存路徑時,保存成功。 當我從用戶那里得到(與代碼中相同的路徑)時,出現以下錯誤:
Access to the path is denied
這是我的保存功能(兩種功能都相同):
public void SaveFile(string path)
{
try
{
XmlSerializer serializer = new XmlSerializer(typeof(List<MyClass>));
TextWriter textWriter = new StreamWriter(path);
serializer.Serialize(textWriter, MyList);
textWriter.Close();
}
catch (Exception e)
{
}
}
從用戶我發送到此功能如下:
public void UserSave()
{
//Open dialog in the path that i have in the code:
fileDialog.InitialDirectory = MyPath;
if (fileDialog.ShowDialog() == DialogResult.OK)
{
SaveFile(Path.GetDirectoryName(fileDialog.FileName));
}
}
可能是什么問題呢?
我在以下行中發現了錯誤:
SaveFile(Path.GetDirectoryName(fileDialog.FileName));
它基本上將其保存為文件夾而不是文件 ,因此它掉了。
我將其更改為:
SaveFile(fileDialog.FileName);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.