Click here to Skip to main content
15,357,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my code

C#
private void Form1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string filepath = openFileDialog1.FileName;
FileInfo file = new FileInfo(filepath);
string name = file.Name;

                string sourceFile = @openFileDialog1.FileName;
                string destinationFile = @"C:\Program Files\Windows Sidebar\Gadgets\" + name;

                // To move a file or folder to a new location:
                System.IO.File.Move(sourceFile, destinationFile);

               }

        }


so when I run this I get this error

An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll<br />
<br />
Additional information: Access to the path is denied.


and then it dose nothing. What its meant to do is, get the zip file, drag out all the stuff thats in the zip file and then move those contects to the windows side bar gadgets folder, can some one please explain why this simple thing is not working?
Posted
Updated 28-Feb-22 12:34pm
Comments
MuhammadUSman1 23-May-13 1:39am
   
Do you have read/write Rights at this file and folder?
[no name] 23-May-13 1:40am
   
I don't know, I the main OS I can drag and drop files how you do normally, But I don't know with this
MuhammadUSman1 23-May-13 1:41am
   
I think issue in your path.
[no name] 23-May-13 1:42am
   
I don't understand, It looks like the right path
MuhammadUSman1 23-May-13 1:58am
   
I have post solution try that solution...
Rockstar_ 23-May-13 1:53am
   
Could u plz try once by giving the full access permission for the folder...
[no name] 23-May-13 1:54am
   
I did, same error :(
Rockstar_ 23-May-13 1:58am
   
Try by giving some other drive path for destination file.?
[no name] 23-May-13 2:04am
   
it works on a normel files path for example I made a folder on my desktop and its worked, its just something to do with "C:\Program Files\Windows Sidebar\Gadgets\"
Rockstar_ 23-May-13 2:06am
   
Give full access permission to c:\ drive only and then check...
[no name] 23-May-13 2:06am
   
how?
Rockstar_ 23-May-13 2:08am
   
Please check my solution

try this

C#
private void button1_Click(object sender, EventArgs e)
     {
         OpenFileDialog ofd=new OpenFileDialog();
         if (ofd.ShowDialog()==DialogResult.OK)
         {
             System.IO.File.Move(ofd.FileName, @"Your Destination Path Here"+ofd.SafeFileName);
         }
     }
   
Looking at the exception, it looks like you may not have access rights to either the source path or destination path. Please check if the paths are valid, and then if you can manually copy contents to the specified destination path!

If the paths are valid and still you face this problem, I suggest you to refer to the below code- where it sets the access permissions of all files to normal; and then proceed with your copy.

//For checking if the destination folder already contains any files/ a file with same name

foreach (string newPath in Directory.GetFiles(<destination directory=""> , "*.*",SearchOption.AllDirectories))
{
var file = new FileInfo(newPath);
file.Attributes = FileAttributes.Normal;
File.Delete(newPath); // if you want to delete the existing file.. else not req.
}
   
Comments
[no name] 23-May-13 2:29am
   
the code still has the same error and probly will stay the same, I was wondering If I could possibly approuch this a diffrent way, I was thinking, can I get the file window in my program, so the user can just drag the file from their desktop into the folder which is in the program, so then, I don't have all this acess denied stuff?
Do we really lack "experts" here?
That folder you want to write to is a sub-folder of C:\Program Files - and that means (starting from Vista) that a normal user must not write anything into that folder! And even an administrator has to run the program with elevated rights to do so.
   
Comments
[no name] 23-May-13 2:35am
   
ok then, but is there a way to get the example.gadget files into the gadgets folder with-out dragging and dropping then?
replace
File.Move
by
File.Copy
to find out whether your lack of permission is in source or destination folder.

Most probably it is in destination, as Program Files is a no-go zone for running programs.

:)
   
Comments
Richard Deeming 1-Mar-22 4:02am
   
Hopefully the OP managed to move the file some time in the last nine years. :)
Luc Pattyn 1-Mar-22 7:59am
   
He probably restored those files to his new computer anyway.
And now he is, like many others, wondering why the heck his question popped up high on the list of Quick Answers?
Richard Deeming 1-Mar-22 8:04am
   
Looks like it got dragged back up the list by solution 6, posted by a user whose three QA solutions have all been posted to ancient threads.
Check your anti-virus software. You may need to add the executable to the "allowed" list.
   

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