Permission to access file from program!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Permission to access file from program!

  1. #1
    Join Date
    Jun 2012
    Posts
    37

    Question Permission to access file from program!

    Hi There! How are you?

    I have a program that I must load a certain image depending of certain conditions. This image can not be built into resources file, it must be separated from the program and it is actually residing in the same directory as the .exe file.

    So, my code is really simple and tries to access the image file from the disk:

    Code:
                                    System::IO::FileStream ^fs;
    
    				if(tabPage8->Size.Width > 996)
    					fs = gcnew FileStream("fundo_tp8_zoom2.png", FileMode::Open); //Opens the file
    				else
    					fs = gcnew FileStream("fundo_tp8.png", FileMode::Open); //Opens the file
    
    				unsigned int tamanho = safe_cast<unsigned int>(fs->Length); //Gets the file size
    			        array<unsigned char>^ file_array = gcnew array<unsigned char>(tamanho);	//Creates an array with the same size 
    
    				fs->Read(file_array,0, tamanho);	//Reads file into the array
    				fs->Close();	//Closes file
    
    				MemoryStream^ ms = gcnew MemoryStream(file_array);	//Creates a memory stream from the array
    				Bitmap^ MyImage = gcnew Bitmap(ms);	//Converts stream into bitmap
    
    				tabPage8->BackgroundImage = MyImage; //Changes the background image
    It works fine, but the problem is that unless it is executed with Administrator privileges, windows denies the access and the program crashes. I'd like that to run without administrator privileges, so I won't have to explain to every customer how to do it. How could I possibly work around this problem?

    Thank you!

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,588

    Re: Permission to access file from program!

    Not even the application itself is granted write access to its own application directory without admin rights under current Windows versions. However, the FileStream c'tor you're using defaults to requesting read/write access, so it's no surprise it fails. Try passing FileAccess::Read as the third patameter.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Jun 2012
    Posts
    37

    Re: Permission to access file from program!

    Quote Originally Posted by Eri523 View Post
    Not even the application itself is granted write access to its own application directory without admin rights under current Windows versions. However, the FileStream c'tor you're using defaults to requesting read/write access, so it's no surprise it fails. Try passing FileAccess::Read as the third patameter.
    Eri, you're GREAT! It worked! Thanks a lot!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center