文件已存在 [英] File.Exists

查看:66
本文介绍了文件已存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我的问题是如果我的文件在我的本地

驱动器上,File.Exists工作正常,但如果它在任何其他驱动器上则返回false驾驶。我认为

问题可能是文件权限,所以我尝试了以下内容:

FileIOPermision permFileIO = new

FileIOPermission (FileIOPermissionAccess.Read,

filepath.Value.ToString());


试试

{

permFileIO.Demand();

}

catch(例外e)

{

}


这样可行但是如何捕获没有

权限读取文件的用户的例外?


谢谢

Sur

Hello,

My problem is that File.Exists works fine if my file is on my local
drive but returns false if its on any other drive. I think that the
issue is probably file permissions and so I have tried the following:

FileIOPermision permFileIO = new
FileIOPermission(FileIOPermissionAccess.Read,
filepath.Value.ToString());

try
{
permFileIO.Demand();
}
catch(Exception e)
{
}

This works but how do I catch an exception of the user not having the
right permissions to read the file?

Thanks
Sur

推荐答案

>我的问题是如果我的文件在我的本地
> My problem is that File.Exists works fine if my file is on my local
驱动器上,File.Exists工作正常,但如果它在任何其他驱动器上则返回false。我认为
问题可能是文件权限,所以我尝试了以下内容:

FileIOPermision permFileIO = new
FileIOPermission(FileIOPermissionAccess.Read,
filepath.Value .ToString());

尝试
{
permFileIO.Demand();
}
catch(例外e){
}

这有效,但如何捕获没有
权限读取文件的用户的例外?
drive but returns false if its on any other drive. I think that the
issue is probably file permissions and so I have tried the following:

FileIOPermision permFileIO = new
FileIOPermission(FileIOPermissionAccess.Read,
filepath.Value.ToString());

try
{
permFileIO.Demand();
}
catch(Exception e) {
}

This works but how do I catch an exception of the user not having the
right permissions to read the file?




将异常替换为您想要捕获的异常:


尝试{

permFileIO.Demand();

} catch(MyExceptionIWantToCatch e){

//错误

}


可能是程序没有LAN或Internet访问权限权利

(程序员的责任),还有那个

计算机上的管理员不喜欢访问局域网或Internet驱动器的程序。我相信这个是你需要的那个

System.Security.Policy.PolicyException


try {

permFileIO.Demand();

} catch(PolicyException e){

//报告此机器上没有足够的用户权限

}


技术上我相信C#你可能想要嵌套异常

试试{

试试{

permFileIO.Demand();

} catch(PolicyException e){

//报告此机器上没有足够的用户权限

}

} catch(例外e){

//典型异常错误报告

}

}


但你可能也会像这种做法一样使用:

如果异常是PolicyException,那么第二部分就不会执行了。

但如果不是PolicyException则执行异常


尝试{

尝试{

permFileIO.Demand ();

} catch(PolicyException e){

//在这台机器上报告用户权限不足

} catch(例外e){

//典型异常错误报告

}

}


(我希望我写得正确)



Replace the Exception to the exception you want to catch like this:

try {
permFileIO.Demand();
} catch(MyExceptionIWantToCatch e) {
// error
}

It might be that the program have no LAN or Internet access rights
(resposibility of the programmer), but also that the administrator on that
computer does not like the program to access the LAN or Internet drive. I
believe that this one is the one you need
System.Security.Policy.PolicyException

try {
permFileIO.Demand();
} catch(PolicyException e) {
// Report not enough user rights on this machine
}

Technically I believe in C# you might want to nest exceptions
try {
try {
permFileIO.Demand();
} catch(PolicyException e) {
// Report not enough user rights on this machine
}
}catch(Exception e) {
// Typical exception error reporting
}
}

But you might also use like this fall-through way of doing it:
If the exception was PolicyException then the second part is not executed.
But if it was not PolicyException then Exception is executed

try {
try {
permFileIO.Demand();
} catch(PolicyException e) {
// Report not enough user rights on this machine
} catch(Exception e) {
// Typical exception error reporting
}
}

(I hope I wrote it correctly)




" sur" <苏************ @ hotmail.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...

"sur" <su************@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
你好,

我的问题是如果我的文件在我的本地
驱动器上,File.Exists工作正常,但如果它在任何其他驱动器上则返回false。我认为
问题可能是文件权限,所以我尝试了以下内容:

FileIOPermision permFileIO = new
FileIOPermission(FileIOPermissionAccess.Read,
filepath.Value .ToString());

尝试
{
permFileIO.Demand();
}
catch(例外e)
{
}

这有效,但如何捕获没有
权限读取文件的用户的例外?

谢谢
Sur
Hello,

My problem is that File.Exists works fine if my file is on my local
drive but returns false if its on any other drive. I think that the
issue is probably file permissions and so I have tried the following:

FileIOPermision permFileIO = new
FileIOPermission(FileIOPermissionAccess.Read,
filepath.Value.ToString());

try
{
permFileIO.Demand();
}
catch(Exception e)
{
}

This works but how do I catch an exception of the user not having the
right permissions to read the file?

Thanks
Sur




你很困惑(Olaf也是如此),Windows安全和代码访问

安全性,你的问题与第一个问题有关也就是说,USER运行

代码无权访问资源(网络共享/文件)。

访问网络共享由Windows安全控制系统

(通过USER的访问令牌),因此是Windows安全问题。

FileIOPermission是CAS权限,这里是CLR' 安全系统

检查CODE是否已被授予FileIO权限。虽然

完全不同,但它们相互补充,所以现在你可以对资源进行代码

访问和用户访问控制。


Willy。



You are confusing (and so does Olaf), Windows security and Code Access
Security, your problem is related to the first, that is, the USER running
the code has no permissions to access the resource (network share/file).
Access to a network share is controlled by the Windows security system
(through the USER''s access token) and as such is a Windows security issue.
FileIOPermission is a CAS permission, here it''s the CLR''s security system
that checks whether the CODE has been granted FileIO permissions. While both
are totally different, they complement each other, so now you can have ''Code
access'' and ''User access'' control to a resource.

Willy.


>你很困惑(Olaf也是如此),Windows安全和代码访问
> You are confusing (and so does Olaf), Windows security and Code Access
安全性,你的问题与第一个有关,也就是说,运行的用户运行代码没有访问权限资源(网络共享/文件)。
访问网络共享由Windows安全系统控制
(通过USER的访问令牌),因此是Windows安全问题。 FileIOPermission是一个CAS权限,这里是CLR的安全系统,用于检查CODE是否已被授予FileIO权限。虽然
两者完全不同,但它们相互补充,所以现在你可以对资源进行
'代码访问'和''用户访问''控制。
Security, your problem is related to the first, that is, the USER running
the code has no permissions to access the resource (network share/file).
Access to a network share is controlled by the Windows security system
(through the USER''s access token) and as such is a Windows security issue.
FileIOPermission is a CAS permission, here it''s the CLR''s security system
that checks whether the CODE has been granted FileIO permissions. While both are totally different, they complement each other, so now you can have ''Code access'' and ''User access'' control to a resource.



我在谈论.NET管理员没有给LAN权限

让.NET程序访问网络文件夹。

当然你也有考虑到共享文件夹必须具有足够的共享访问权限,并且该

计算机上的文件/文件夹权限设置为您可以读取的方式那个文件夹。


我假设他的Windows权限和共享权限都没问题,

因为这是第一个检查可能是用记事本打开文件。


我们有4个位置可以解决问题:

1.文件/文件夹没有足够的Windows权限。

- >共享机器的管理员

2.文件/文件夹共享没有enoygh权限。

- >共享机器的管理员

3.程序员编译托管程序忘了告诉程序



应该能够访问共享局域网文件夹。

4.程序必须运行的机器管理员应该给

管理

程序权限去寻找一个共享的局域网文件夹。


大多数程序员在第3点和第4点失败,因为他们不知道这些是新的,并且应该考虑到它们。


因为在他的代码示例中他试图动态地要求读取访问权限,并且他获得了异常,这表明该程序正在运行

无权访问共享的LAN驱动程序,因此该运行的计算机上的

管理员可以告诉.NET CLI是否可信任

并且允许访问。默认它是关闭的,所以这解释了'

为什么它在本地工作但不能远程工作。


但我承认有时候很难理解这个所有。



I was talking about the .NET administrator that does not give LAN rights to
let the .NET program access the network folder.
Of course you also have to take into account that a Shared folder must have
enough share access rights and that the file/folder permissions on that
machine are set in such a way that you could read that folder.

I was assuming that his windows permissions and share rights were ok,
because that is the first to check maybe by opening the file with notepad.

We have 4 locations that could give the problem:
1. The file/folder have not enough Windows permission.
-> administrator of the shared machine
2. The file/folder share have not enoygh permission.
-> administrator of the shared machine
3. The programmer compiling the managed program forgot to tell the program
that
is should be able to access shared LAN folders.
4. The Administrator of the machine the program must run on should give the
manage
programs rights to go and look for a shared LAN folder.

Most programmers fail point 3 and 4 since they do not know that these are
new for them and should be taken into account.

Since in his code sample he is trying to dynamically demand read access, and
he gets an exception, that would suggest that the program running on that
have no rights to access a shared LAN driver, so it is up to the
administrator on that running machine to tell the .NET CLI is to be trusted
and is allowed to have access. Default it is turned off, so this explain''s
why it works locally but not remotely.

But I admit it is sometimes confusing to understand tihis all.



这篇关于文件已存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆