C# FileStream无法使用相对路径

C# FileStream无法使用相对路径,c#,filestream,C#,Filestream,我试图使用具有相对路径的文件流,但它不起作用 var pic = ReadFile("~/Images/money.png"); 当我使用以下东西时,它就起作用了: var p = GetFilePath(); var pic = ReadFile(p); 代码的其余部分(来自SO): 我不明白为什么它不工作,因为FileStream构造函数允许使用相对路径 我假设程序中的文件夹包含子文件夹images,其中包含您的图像文件 \folder\program.exe \folder\Imag

我试图使用具有相对路径的文件流,但它不起作用

 var pic = ReadFile("~/Images/money.png");
当我使用以下东西时,它就起作用了:

var p = GetFilePath();
var pic = ReadFile(p);
代码的其余部分(来自SO):


我不明白为什么它不工作,因为FileStream构造函数允许使用相对路径

我假设程序中的文件夹包含子文件夹images,其中包含您的图像文件

\folder\program.exe

\folder\Images\money.jpg


尝试不使用“~”

我也遇到了同样的问题,但我通过使用此代码解决了它

试试这个代码,希望它也能解决你的问题

 #region GetImageStream
    public static Stream GetImageStream(string Image64string)
    {
        Stream imageStream = new MemoryStream();
        if (!string.IsNullOrEmpty(Image64string))
        {
            byte[] imageBytes = Convert.FromBase64String(Image64string.Substring(Image64string.IndexOf(',') + 1));
            using (Image targetimage = BWS.AWS.S3.ResizeImage(System.Drawing.Image.FromStream(new MemoryStream(imageBytes, false)), new Size(1600, 1600), true))
            {
                targetimage.Save(imageStream, ImageFormat.Jpeg);
            }
        }
        return imageStream;
    }

    #endregion
第二个

 #region GetImageStream
    public static Stream GetImageStream(Stream stream)
    {
        Stream imageStream = new MemoryStream();
        if (stream != null)
        {
            using (Image targetimage = BWS.AWS.S3.ResizeImage(System.Drawing.Image.FromStream(stream), new Size(1600, 1600), true))
            {
                targetimage.Save(imageStream, ImageFormat.Jpeg);
            }
        }
        return imageStream;
    }

    #endregion

~
表示与相对路径不同的虚拟路径。MapPath将其解析为物理路径。这将帮助您了解路径类型之间的差异
 #region GetImageStream
    public static Stream GetImageStream(Stream stream)
    {
        Stream imageStream = new MemoryStream();
        if (stream != null)
        {
            using (Image targetimage = BWS.AWS.S3.ResizeImage(System.Drawing.Image.FromStream(stream), new Size(1600, 1600), true))
            {
                targetimage.Save(imageStream, ImageFormat.Jpeg);
            }
        }
        return imageStream;
    }

    #endregion