简体   繁体   English

Visual Studio 2010对文件的权限

[英]Visual Studio 2010 permissions on files

There is a small program which is supposed to open file and then output it to console adding line numbers. 有一个小程序应该打开文件,然后将其输出到添加行号的控制台。 The problem is that no matter whether program is run from command console of from IDE it throws exception regarding file permission. 问题是,无论程序是否从IDE的命令控制台运行,都会引发有关文件权限的异常。

I moved both executable and the file which is supposed to be read (simple TXT file) to several directories (my document, temp, etc) run console as Admin, run Visual studio as admin, gave all permissions to both files, but it always throws exception. 我将可执行文件和应该读取的文件(简单的TXT文件)都移到了几个目录(我的文档,临时文件等)中,以Admin身份运行控制台,以admin身份运行Visual Studio,并赋予了这两个文件所有的权限,但始终引发异常。 The strangest thing is that a week or two ago I fund solution by trial and error but but I can' remember it. 最奇怪的是,一两个星期前,我通过反复试验为解决方案提供资金,但我记不得了。

Here is exception: 这是例外:

    Exception: System.UnauthorizedAccessException: Access to the path 'C:\Users\Nena
d\documents\visual studio 2010\Projects\Listing 10.6\Listing 10.6\bin\Debug\prog
ram.cs' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, I
nt32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions o
ptions, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolea
n useLongPath)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access,
FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean
bFromProxy)
   at System.IO.FileStream..ctor(String path, FileMode mode)
   at ListFile.Main(String[] args) in C:\Users\Nenad\documents\visual studio 201
0\Projects\Listing 10.6\Listing 10.6\Program.cs:line 22


Press any key to continue . . .

Here is code: 这是代码:

// ListFile.cs - program to print a listing to the console
//-----------------------------------------------------------

using System;
using System.IO;

class ListFile
{
    public static void Main(string[] args)
    {
        try
        {

            int ctr = 0;
            if (args.Length <= 0)
            {
                Console.WriteLine("Format: ListFile filename");
                return;
            }
            else
            {
                FileStream fstr = new FileStream(args[0], FileMode.Open);
                try
                {
                    StreamReader t = new StreamReader(fstr);
                    string line;
                    while ((line = t.ReadLine()) != null)
                    {
                        ctr++;
                        Console.WriteLine("{0}:  {1}", ctr, line);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception during read/write: {0}\n", e);
                }
                finally
                {
                    fstr.Close();
                }
            }
        }

        catch (System.IO.FileNotFoundException)
        {
            Console.WriteLine("ListFile could not find the file {0}", args[0]);
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception: {0}\n\n", e);
        }
    }
}

Check one of these possibilities: 检查以下可能性之一:

  • File is not open in any other window/application 文件未在任何其他窗口/应用程序中打开
  • Run your applications .exe file as Administrator (optional extra, enable UAC so that you will see the request that the application requires elevated privileges and to explicitly give them, in Windows8 disabling UAC only hides these popups but that doesn't mean the application will have elevated rights so be careful if using Win8) as Administrator运行应用程序.exe文件(可选,请额外启用UAC,以便您可以看到该应用程序要求提升权限并明确授予它们的请求,在Windows8中,禁用UAC仅会隐藏这些弹出窗口,但这并不意味着该应用程序会具有较高的权利,因此如果使用Win8,请当心)
  • Manually set read rights to Everyone for that file 手动为该文件的Everyone设置读取权限
  • Check that the file is not in a special folder (but i think you already did that, but just to be sure create c:\\temp and put it there) 检查文件是否不在特殊文件夹中(但我认为您已经这样做了,但请确保创建c:\\ temp并将其放在此处)

CAUTION - The exception shows that there was a problem accessing C:\\Users\\Nena d\\documents\\visual studio 2010\\Projects\\Listing 10.6\\Listing 10.6\\bin\\Debug\\prog ram.cs not the a simple text file!!! 注意-异常表明访问C:\\Users\\Nena d\\documents\\visual studio 2010\\Projects\\Listing 10.6\\Listing 10.6\\bin\\Debug\\prog ram.cs而不是简单的文本文件!

Be careful you may be providing a wrong path in your code by accident. 请注意,您可能偶然在代码中提供了错误的路径。 And the Users folder is a special folder which requires elevated privileges to access, so better move the whole executable + readableFile to an ordinary folder where it will not encounter problems (like the c:\\temp i mentioned above) 而且, Users文件夹是一个特殊的文件夹,需要提升的特权才能访问,因此最好将整个可执行文件+可读文件移动到不会遇到问题的普通文件夹中(例如上面提到的c:\\ temp)

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

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