0

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.

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. 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);
        }
    }
}
4

1 回答 1

1

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)
  • Manually set read rights to Everyone for that file
  • 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)

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!!!

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)

于 2013-02-28T07:49:28.063 回答