c# - 获取非 URI 位置

标签 c# io uri

当我这样做的时候

string s = Path.Combine(Folders.Data, fileName);

我有 file:\\... (URI 位置)。如何获取 c:\...(非 URI 位置)?

编辑:

Folders 类的代码是:

public static class Folders
{
    public static string App
    {
        get
        {
            return Path.GetDirectoryName(
                Assembly.GetAssembly(typeof(Folders)).CodeBase
                );
        }
    }

    public static string Data
    {
        get
        {
            return Path.Combine(App, "Data");
        }
    }
}

最佳答案

Path.Combine 添加最后一个组件,您需要从Folders.Data 中删除 file:\

你可以做这样的事情

string s = Path.Combine(Folders.Data.Replace("file:\\",""), fileName);

要获得清晰的解决方案,请尝试使用 Assembly.Location,如图所示 here这就是你想要的。


所以不要使用:

return Path.GetDirectoryName(
                Assembly.GetAssembly(typeof(Folders)).CodeBase
                );

用途:

return Path.GetDirectoryName(
                Assembly.GetAssembly(typeof(Folders)).Location
                );

关于c# - 获取非 URI 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5330874/

相关文章:

Python 字符串格式化在 print 语句上创建随机新行

java - 如何压缩 URI 图像?

c# - List<T> 容量增加与 Dictionary<K,V> 容量增加?

c# - 我应该在设计中的什么位置放置项目或应用程序范围的 TraceSwitch?

c# - 从 XML 字符串反序列化数组时遇到问题

c# - 如何在 WPF 中将多个组合框与一个数据源一起使用?

c++ - 从文本文件中读取内容,复制它,并放入一个新的文本文件中。 C++

python - 逐行读取文件时出现名称未定义错误python

java - 如何使用 X-Forwarded-* header 创建没有上下文路径的 URI?

java - 为什么我们需要调用 "build()"让 Uri.Builder 对象传入 URL 构造函数?