【发布时间】:2015-06-14 14:26:49
【问题描述】:
我需要从共享文件中获取默认文件图标。我遇到了一些问题,发现the question "How to get the associated icon from a network share file"。
但是现在我遇到了共享文件的另一个问题,看起来好像它们不存在。这是我的代码:
public static Icon ExtractAssociatedIcon(String filePath)
{
int index = 0;
Uri uri;
if (filePath == null)
{
throw new ArgumentException(String.Format("'{0}' is not valid for '{1}'", "null", "filePath"), "filePath");
}
try
{
uri = new Uri(filePath);
}
catch (UriFormatException)
{
filePath = Path.GetFullPath(filePath);
uri = new Uri(filePath);
}
if (uri.IsFile)
{
if (!File.Exists(filePath))
{
throw new FileNotFoundException(filePath);
}
StringBuilder iconPath = new StringBuilder(260);
iconPath.Append(filePath);
IntPtr handle = SafeNativeMethods.ExtractAssociatedIcon(new HandleRef(null, IntPtr.Zero), iconPath, ref index);
if (handle != IntPtr.Zero)
{
return Icon.FromHandle(handle);
}
}
return null;
}
我总是收到FileNotFoundException,但我现在不知道为什么。 filePath 可以,我可以通过资源管理器访问这些文件。我试图从filePath 创建FileInfo 实例(我在某处读到它可能会有所帮助)但仍然没有。
我错过了什么?
【问题讨论】:
-
您应该可以访问网络位置,并且您的文件路径应该是这样的:
\\ServerName\FolderName\FileName.txt和 C# 中的@"\\ServerName\FolderName\FileName.txt" -
stackoverflow.com/questions/17472168/… 的可能重复项(即使现在的问题是关于
File.Exists而旧问题是关于Directory.Exists)。 -
我猜你是在像 IIS 这样的进程中运行,并且运行的权限没有网络资源的访问权限。
标签: c# network-shares