Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
static void Main(string[] args)
        {
            
            File.Move(@"c:\windows\a.txt",@"c:\windows\b.txt");
            Console.Write("done");
        }
Posted
Comments
anandd.mohit 29-Aug-14 4:27am    
private void Form1_Load(object sender, EventArgs e)
{

Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(@"c:\windows\a.txt",@"a.txt");
MessageBox.Show("done");
}

error again same as previous

The error explains it; you are unauthorized to move the file, you don't have access. To solve this, you have to run your program as administrator.
 
Share this answer
 
v2
The Windows directory is a system folder: you need full authority to make any changes there, and renaming a file is a change: this is a security precaution to reduce the damage that "rogue applications" can do. You cannot bypass this except to elevate your application, which will require the UAE and user approval.

Please, don't store text files in any old folder: use a sensible place that will remain usable in future versions of the operating system.
This should help: Where should I store my data?[^]
 
Share this answer
 
v2
Hi First try in local drive like D,E ,other than OS drive it will work or not ?
or

Try this code
C#
Use the VB assembly in C# Add reference to Microsoft.VisualBasic

then to rename a file

Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(myfile, newName);

Both are strings, note that myfile has the full path. newName does not. for example

a="C:\whatever\a.txt";
b="b.txt";
Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(a, b);
C:\whatever\ folder will now contain b.txt


Regards
Aravind
 
Share this answer
 
thanks for reply
i already run it as admin.
 
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