简体   繁体   English

C#File.exists返回false

[英]C# File.exists returns false

I have a program that opens a database, the path to that database is this: 我有一个打开数据库的程序,该数据库的路径是这样的:

private static string strDefaultDB2 = @"C:\Users\" + Environment.UserName + @"\OneDrive\TIME FILE\MyName\TimeFile.accdb";

All computers I have tried this one were able to access it fine except one, it also has access to OneDrive but when I run the code the file doesn't exist... If I enter the path generated by the code which is something like this: 我尝试过的所有计算机都可以访问它,除了一台计算机,它也可以访问OneDrive,但是当我运行代码时,该文件不存在...如果输入由代码生成的路径,例如这个:

C:\Users\UserName\OneDrive\TIME FILE\MyName\TimeFile.accdb

In explorer, it opens up the database right away. 在资源管理器中,它将立即打开数据库。 I'm not sure why it can't find the database on this one machine... I also tried running it as admin but that didn't change anything 我不确定为什么它无法在这台机器上找到数据库...我也尝试过以admin身份运行它,但是没有任何改变

This is the code that executes: 这是执行的代码:

 if (!System.IO.File.Exists(doesFileExist))
            {

                Polaris.Polaris.log("The Path " + doesFileExist + " Does Not Exist!");

                runWindowDB();


            }

If I try to do a rename on the file it throws an exception: "Could not find file [path]" 如果我尝试对文件进行重命名,则会引发异常:“找不到文件[路径]”

And If I try to open the database: "[path] is not a valid path" 而且,如果我尝试打开数据库:“ [路径]不是有效路径”

I don't get it because this path has whitespace on every pc and the only thing that changes is the username. 我不明白,因为此路径在每台PC上都有空格,唯一改变的是用户名。 Explorer opens the file so the path is correct. 资源管理器将打开文件,因此路径正确。

Any ideas? 有任何想法吗?

Thanks 谢谢

Do you get an exception? 你有例外吗? Can you read/write this file? 您可以读取/写入此文件吗? You have a whitespace in the string I don't think that is good anyway. 您在字符串中有一个空格,但我认为这还是不好的。

From the official documentation link : 从官方文档链接

If path describes a directory, this method returns false. 如果path描述目录,则此方法返回false。 Trailing spaces are removed from the path parameter before determining if the file exists. 在确定文件是否存在之前,将从path参数中删除尾随空格。

The Exists method returns false if any error occurs while trying to determine if the specified file exists. 如果在尝试确定指定文件是否存在时发生任何错误,则Exists方法将返回false。 This can occur in situations that raise exceptions such as passing a file name with invalid characters or too many characters, a failing or missing disk, or if the caller does not have permission to read the file. 在引发异常的情况下可能会发生这种情况,例如传递带有无效字符或太多字符的文件名,磁盘出现故障或丢失,或者调用者无权读取文件。

Hard code another users username that you know works, try it from this machine, msdn mentions it will return false if there are invalid characters in the string or if the path is determined to be a folder, there might be some odd encoding going on or virus scanner intercepting the io. 硬编码另一个您知道有效的用户名,可以在这台机器上尝试使用,msdn指出,如果字符串中包含无效字符,或者如果确定路径为文件夹,则它将返回false,这可能是由于某些奇怪的编码或病毒扫描程序拦截了io。 If that fails try getting the user to log onto another box that works and if the file is then recognized it must the environment on that users original box. 如果失败,请尝试使用户登录到另一个可以使用的框,然后识别该文件,它必须是该用户原始框上的环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM