0

我設置字符串變量從路徑+文件名與Environment.GetFolderPath(Environment.SpecialFolder,這是文件沒有任何擴展 是在Windows Server 2003和XP上正常工作但不工作在Windows 7和Windows Server 2008文件存在與特殊文件夾不工作在Windows七

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

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

你確定你的文件在system32文件夾中,而不是在系統中? – VladL 2013-03-03 08:21:48

回答

2

請檢查兩件事情:

  • 是應用有權系統目錄
  • 是個文件文件夾確實存在?

工作示例(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(); 
     } 
    } 
} 
+0

是的,我是檢查文件的完整路徑與斷點和運行菜單打開,並設置兼容性作爲管理員運行,但dosnt工作在Windows 7和服務器2008年 – 2013-03-03 08:45:57

0

Windows 7或2008服務器x64平臺上有兩個文件夾,SYSTEM32和.NET回報systemWOW64 systemWOW32特殊的文件夾類作爲SYSTEM32,當我們使用specialfolder。系統。返回系統WOW64。

0

如果您的過程沒有權限,File.Exists將返回false並且不會引發異常。

嘗試打開或枚舉本地目錄中的文件,看看您是否得到UnauthorizedAccessException

相關問題