【问题标题】:file exist with special folder not working on windows seven文件存在特殊文件夹在 Windows 7 上不起作用
【发布时间】:2013-03-03 08:16:35
【问题描述】:

我使用 Environment.GetFolderPath(Environment.SpecialFolder 从路径 + 文件名设置字符串变量,该文件没有任何扩展名 在 windows server 2003 和 xp 上可以正常工作,但在 windows 7 和 windows server 2008 上不能正常工作

var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "testxyz");

if (File.Exists(path))            
{return true;}
else
{return false;}

【问题讨论】:

  • 你确定你的文件是在system32文件夹里而不是在系统里吗?

标签: c# windows-7 windows-server-2008 filepath windows-applications


【解决方案1】:

请检查两件事:

  • 应用程序是否有权访问系统目录?
  • 这个文件夹下的文件真的存在吗?

工作示例(win7 32 位):

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();
        }
    }
}

【讨论】:

  • 是的,我用断点检查文件的完整路径并使用运行菜单打开,并设置了与以管理员身份运行的兼容性,但在 windows 7 和 server 2008 上不工作
【解决方案2】:

windows 7 或 2008 服务器 x64 平台有两个文件夹 system32 和 systemWOW32 特殊文件夹类在 .net 返回 systemWOW64 作为 system32 和当我们使用 specialfolder.system 。返回系统WOW64。

【讨论】:

    【解决方案3】:

    如果您的进程没有权限,File.Exists 将返回 false 并且不会抛出异常。

    尝试打开或枚举本地目录中的文件,看看是否得到UnauthorizedAccessException

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-21
      • 2023-04-01
      • 1970-01-01
      相关资源
      最近更新 更多