不支持URI格式C# [英] URI formats are not supported C#

查看:803
本文介绍了不支持URI格式C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了ac#应用程序并且我想要删除Bin / Debug文件夹中的数据库文件并复制其中的另一个数据库。但是当我想要这样做时,我不支持异常URI格式。这是我的代码:

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);
           }





我尝试过:



i试了一下:



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

推荐答案

如果没有你的代码在你的系统上运行,我们无法做任何帮助 - 我们不知道这里汇集了什么路径,但是......如果你看一下Codebase属性的定义: AssemblyName.CodeBase Property(System.Reflection) [ ^ ]它清楚地说:

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:

获取或设置的位置汇编为URL。

Gets or sets the location of the assembly as a URL.

由于File.Delete和File.Copy不接受基于URL的路径,因此您需要一个更好的系统。



说实话,你将数据库存放在完全错误的地方:执行组件通常存储在app文件夹(生产中通常位于Program Files下)或Windows目录下。由于安全原因,这两者通常都需要管理员访问才能修改任何文件。而那些oftens意味着一个应用程序在生产中失败,但在开发中工作。

看看这里:我应该在哪里存储我的数据? [ ^ ] - 它表示更好的地方。

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.


这篇关于不支持URI格式C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆