【问题标题】:Accessing the images folder in wpf app访问 wpf 应用程序中的图像文件夹
【发布时间】:2014-10-03 14:02:19
【问题描述】:

我有一个 wpf 应用程序,我想在其中将文件保存在 Images 文件夹中,该文件夹与 bin 文件夹处于同一级别。

如何访问该文件夹?

我尝试这样:

using (var imageFile = new FileStream((new Uri(@"pack://application:,,,/Images/Application/")).ToString(), FileMode.Create))
                {
                    //
                }

我收到“不支持给定路径的格式”的错误。如何访问该文件夹?

【问题讨论】:

    标签: wpf path


    【解决方案1】:

    您的路径是指项目程序集中的资源。您应该使用相对或绝对文件系统路径:

    var path = Path.Combine(
        Path.DirectorySeparatorChar.ToString(), 
        AppDomain.CurrentDomain.BaseDirectory, 
        "Images"); // Result could be for example: "C:\MyWorkspace\MyProject\bin\Debug\Images"
    
    using (var imageFile = new FileStream(path, FileMode.Create)) { }
    

    【讨论】:

    • 我说Images文件夹和bin文件夹在同一层,不在bin\Debug里面。
    • AppDomain.CurrentDomain.BaseDirectory 返回您的应用程序基目录,您的可执行文件从该目录运行。使用双点 (@"..\Images") 指向父目录。为什么还要将图像放入 bin 文件夹中?
    【解决方案2】:

    如何使用双点(..)到达父目录。

    使用 (var imageFile = new FileStream("..\..\[Path goes here]", FileMode.Create)) { // }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-03
      • 2018-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多