Click here to Skip to main content
15,922,696 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hey guys,

I asked a question yesterday and recieved an answer which was 100 % accurate and what i am after.

I need some more help.


Basically in my project i want to place a embedded file to program files\test\

The method i am using is this
[code]
C#
public static void SaveResource(string resourceName, string fileName)
{
    Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
    using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
    {
        byte[] b = new byte[stream.Length + 1];
        stream.Read(b, 0, Convert.ToInt32(stream.Length));
        fileStream.Write(b, 0, Convert.ToInt32(b.Length - 1));
        fileStream.Flush();
        fileStream.Close();
    }
    stream.Close();
}

[/code]

and after i have done the above i just add to w/e button i want to move the file with this:

[code]SaveResource("WindowsFormsApplication1.test.exe", @"C:\Program Files\test")[/code]

Now this all seems to work fine. I first try debug in C# and then open the exe as an admin and try it and i get an error saying thati dont have rights to move it? I think that is User Account Control preventing it from being allowed. Is their a way to make it possible of this to be allowed to be moved? I even tried my desktop and it wans't allowed ? UAC isnt even on medium settings here. Is something in the above incorrect?

I also added at the start Using System.IO and Using System.Reflection, The program is ran as admin, The test.exe is a embedded resource.
Posted
Comments
Suvabrata Roy 11-Jul-12 6:59am    
your code seems to have ok.

Are you add Resource's as Public ?

If So then Check user permission at Folder :"C:\Program Files\test"

UAC will interfere with any attempt to write to a path in Program Files. Why are you trying to write there? ... it seems like an extracted resource is probably a temporary thing and you ought to be extracting it to the user's space (whether temporary folder or Local Application Data depends on whether you want a persistent cached version).

If you're running under the debugger, the debugger would need to be running as an administrator to have access there.
 
Share this answer
 
Comments
burgo855 12-Jul-12 5:08am    
Yes, i run the debugger as an administrator.

I am not writing anyhting i am moving an embeeded resource on the debug to program files. The files i am moving are updated files. The old files do not even exist anymore and i wanted to make a program that updated a folder of mine as otherwise i have to manually do it all the time. I am also going to use this on my other 3 computers. However, even if ran as an admin it wont even place the file on my desktop?

Here is the error:
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.UnauthorizedAccessException: Access to the path 'C:\Users\NAME\Desktop' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean 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 test.Form1.SaveResource(String resourceName, String fileName) in C:\Users\NAME\documents\visual studio 2010\Projects\test\test\Form1.cs:line 180
at test.Form1.button4_Click(Object sender, EventArgs e) in C:\Users\NAME\documents\visual studio 2010\Projects\test\test\Form1.cs:line 196
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.269 (RTMGDR.030319-2600)
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
test
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Users/NAME/Documents/Visual%20Studio%202010/Projects/test/test/bin/Debug/test.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.278 built by: RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.282 built by: RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.269 built by: RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
<system.windows.forms jitdebugging="true">


When JIT debugging is enabled, any unhandled exception
will be sent to the
Thing is it is also for my mates so like how would i go about making it be able to actually work even with UAC on?

I thought running it as an admin is enough :/ But it doesnt even let me put it on the desktop or anywhere....
 
Share this answer
 
Ill just install the folder to their program files its all good.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900