Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I programmed a c# application and I want to delete a database file that there is in Bin/Debug folder and copy another database inside of it.but when i want do it i have exception URI formats are not supported.here is my code:
 string FileToReplace = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), "bazarganidb.db");
               File.Delete(FileToReplace);
               string OriginalFile =@"D:\bazarganidb.db";
               if(File.Exists(FileToReplace))
               File.Delete(FileToReplace);
               File.Copy(OriginalFile, FileToReplace,true);
              }
           catch (Exception k)
           {
               MessageBox.Show(k.Message);
           }


What I have tried:

i tried it :
string FileToReplace = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(
 System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), "bazarganidb.db");
    
string localPath = new Uri(FileToReplace).LocalPath;

but it doesnt work
Posted
Updated 6-Nov-17 4:17am
v2
Comments
Karthik_Mahalingam 6-Nov-17 9:42am    
place a breakpoint and see what is the value you are getting in FileToReplace

you are deleting without validating the path.
 File.Delete(FileToReplace); // line 3

1 solution

Without your code running on your system, there isn't anything we can do to help - we have no idea what paths are being assembled here, but ... if you look at the defintion of the Codebase property: AssemblyName.CodeBase Property (System.Reflection)[^] it clearly says:
Quote:
Gets or sets the location of the assembly as a URL.
Since File.Delete and File.Copy do not accept URL based paths, you will need a better system.

To be honest, you are storing the DB in totally the wrong place: executing assemblies are normally stored under the app folder (which in production will normally be under "Program Files") or the Windows directory. And both of those will normally require Admin access to modify any files for security reasons. And that oftens means an app fails in production, but works in development.
Have a look here: Where should I store my data?[^] - it suggests better places.
 
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