C# - 在创建的目录上获取 UnauthorizedAccessException
C# - Getting UnauthorizedAccessException on created Directory
在我的应用程序上,我有一个 Config-FileReader - 如果 Temp
目录(在应用程序文件夹中,这里的配置将存储为 Config.json
)存在,这些检查启动,否则应用程序将创建它。这些步骤可以正确运行很长时间。
经过几天的开发,我今天创建了一个简单的文件下载器:
public static void Download(string url, string destination, Callback OnSuccess) {
System.Diagnostics.Debug.WriteLine("[Download] " + url + " to " + destination + " (" + Utils.getFileName(url) + ")");
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.UserAgent] = Software.getName() + "/" + Software.getVersion() + " (" + Software.getOSVersion() + "; " + Software.getOSBit() + "; " + Software.getOSVersionFull() + ")";
client.DownloadDataCompleted += (s, e) =>
{
var text = e.Result;
File.WriteAllText(destination, Path.Combine(destination, Utils.getFileName(url)));
OnSuccess((Object) Path.Combine(destination, Utils.getFileName(url)));
};
client.DownloadDataAsync(new Uri(url));
}
}
当我尝试将文件下载到之前创建的 Temp
目录时,出现 UnauthorizedAccessException
。奇怪的是:该目录已由应用程序成功创建,配置文件将为 created/readed/writed.
但是当我尝试下载文件时:
API.Download(media.URL, Software.getPath() + "Temp", OnSuccess);
下载器的调试输出:
[Download] https://c-ash.smule.com/sf/s26/arr/88/4e/48afbe6c-9aeb-4cc7-82cf-e04d5f0d6b44.mp3 to C:\Users\info\Documents\SharpDevelop Projects\SongManager\SongManager\bin\Debug\Temp (48afbe6c-9aeb-4cc7-82cf-e04d5f0d6b44.mp3)
我没有给出 UnauthorizedAccessException
:
System.UnauthorizedAccessException: Access to the Path "C:\Users\info\Documents\SharpDevelop Projects\SongManager\SongManager\bin\Debug\Temp" was denied.
编辑
当我使用另一个目录时出现同样的问题,该目录将在下载开始前实时创建。
编辑 2
我的第二个想法是,这是一个线程问题,因为 DownloadDataAsync
运行 在另一个线程上。但是
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate {
File.WriteAllText(destination, Path.Combine(destination, Utils.getFileName(url)));
}));
不会解决问题。
我叫恶作剧
File.WriteAllText Method (String, String)
Parameters
path
- Type: System.String
- The file to write to.
contents
- Type: System.String
- The string to write to the file.
您的代码
client.DownloadDataCompleted += (s, e) =>
{
var text = e.Result;
// look at your parameters
File.WriteAllText(destination, Path.Combine(destination, Utils.getFileName(url)));
OnSuccess((Object)Path.Combine(destination, Utils.getFileName(url)));
};
您正在目录上调用 File.WriteAllText
,并将组合路径作为内容传递。
错误应该是,
"Step away from the keyboard young jedi and make your self a coffee! A directory is not a file and you have no business writing to one"
在我的应用程序上,我有一个 Config-FileReader - 如果 Temp
目录(在应用程序文件夹中,这里的配置将存储为 Config.json
)存在,这些检查启动,否则应用程序将创建它。这些步骤可以正确运行很长时间。
经过几天的开发,我今天创建了一个简单的文件下载器:
public static void Download(string url, string destination, Callback OnSuccess) {
System.Diagnostics.Debug.WriteLine("[Download] " + url + " to " + destination + " (" + Utils.getFileName(url) + ")");
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.UserAgent] = Software.getName() + "/" + Software.getVersion() + " (" + Software.getOSVersion() + "; " + Software.getOSBit() + "; " + Software.getOSVersionFull() + ")";
client.DownloadDataCompleted += (s, e) =>
{
var text = e.Result;
File.WriteAllText(destination, Path.Combine(destination, Utils.getFileName(url)));
OnSuccess((Object) Path.Combine(destination, Utils.getFileName(url)));
};
client.DownloadDataAsync(new Uri(url));
}
}
当我尝试将文件下载到之前创建的 Temp
目录时,出现 UnauthorizedAccessException
。奇怪的是:该目录已由应用程序成功创建,配置文件将为 created/readed/writed.
但是当我尝试下载文件时:
API.Download(media.URL, Software.getPath() + "Temp", OnSuccess);
下载器的调试输出:
[Download] https://c-ash.smule.com/sf/s26/arr/88/4e/48afbe6c-9aeb-4cc7-82cf-e04d5f0d6b44.mp3 to C:\Users\info\Documents\SharpDevelop Projects\SongManager\SongManager\bin\Debug\Temp (48afbe6c-9aeb-4cc7-82cf-e04d5f0d6b44.mp3)
我没有给出 UnauthorizedAccessException
:
System.UnauthorizedAccessException: Access to the Path "C:\Users\info\Documents\SharpDevelop Projects\SongManager\SongManager\bin\Debug\Temp" was denied.
编辑
当我使用另一个目录时出现同样的问题,该目录将在下载开始前实时创建。
编辑 2
我的第二个想法是,这是一个线程问题,因为 DownloadDataAsync
运行 在另一个线程上。但是
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate {
File.WriteAllText(destination, Path.Combine(destination, Utils.getFileName(url)));
}));
不会解决问题。
我叫恶作剧
File.WriteAllText Method (String, String)
Parameters
path
- Type: System.String
- The file to write to.
contents
- Type: System.String
- The string to write to the file.
您的代码
client.DownloadDataCompleted += (s, e) =>
{
var text = e.Result;
// look at your parameters
File.WriteAllText(destination, Path.Combine(destination, Utils.getFileName(url)));
OnSuccess((Object)Path.Combine(destination, Utils.getFileName(url)));
};
您正在目录上调用 File.WriteAllText
,并将组合路径作为内容传递。
错误应该是,
"Step away from the keyboard young jedi and make your self a coffee! A directory is not a file and you have no business writing to one"