C# 文件存在,特殊文件夹在windows 7上不起作用
我使用Environment.GetFolderPath(Environment.SpecialFolder)设置path+file name中的字符串变量,该文件没有任何扩展名 这可以在WindowsServer2003和xp上正常工作,但不能在Windows7和WindowsServer2008上工作C# 文件存在,特殊文件夹在windows 7上不起作用,c#,windows-7,windows-server-2008,filepath,windows-applications,C#,Windows 7,Windows Server 2008,Filepath,Windows Applications,我使用Environment.GetFolderPath(Environment.SpecialFolder)设置path+file name中的字符串变量,该文件没有任何扩展名 这可以在WindowsServer2003和xp上正常工作,但不能在Windows7和WindowsServer2008上工作 var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "testxyz");
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "testxyz");
if (File.Exists(path))
{return true;}
else
{return false;}
请检查两件事:
- 应用程序是否有权访问系统目录
- 此文件夹中的文件是否确实存在
windows 7或2008 server x64平台在.net中有两个文件夹system32和systemWOW32特殊文件夹类。当我们使用specialfolder.system.return systemWOW64时,返回systemWOW64作为system32。如果您的进程没有权限,
File.Exists
将返回false,并且不会引发异常
尝试打开或枚举本地目录中的文件,查看是否出现
未经授权的访问异常
是否确定文件位于system32文件夹中而不在系统中?是的,我使用断点检查了文件的完整路径,并使用“运行”菜单打开,设置了与“以管理员身份运行”的兼容性,但在windows 7和windows 7上不起作用服务器2008
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace testPath
{
class Program
{
static void Main(string[] args)
{
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "main.cpl");
Console.WriteLine(File.Exists(path));
Console.ReadLine();
}
}
}