简体   繁体   English

即使我可以看到文件C#,File.Exists也会返回false

[英]File.Exists is returning false even though I can see the file C#

Issue 问题


I have a function which lets the user edit an image when they do this I save this new image to a file to which they I save to the database etc ... 我有一个功能,允许用户在编辑图像时将其保存到文件中,然后将其保存到数据库中,等等。

The issue comes as when I try and look for the file I just saved it says it does not exist but it does? 问题出在我尝试查找刚刚保存的文件时说它不存在,但确实存在吗?

Code


Here I am saving the new file to the TEMP folder: 在这里,我将新文件保存到TEMP文件夹中:

string newFullTempFolderURL = Path.Combine(Global.TempFolder, newFullFileName + ".png");

_image.Save(newFullTempFolderURL, System.Drawing.Imaging.ImageFormat.Png);

At this point when I check the folder the file is in the folder with the new image. 此时,当我检查文件夹时,文件位于带有新图像的文件夹中。

Then when I go on to uploading the file to the server (Using BITS) I do a check to make sure the file exists: 然后,当我继续将文件上传到服务器(使用BITS)时,请进行检查以确保文件存在:

if (File.Exists(Path.Combine(Global.TempFolder + "\\" + newFullFileName)))
{

}

This then returns false ( Not exists ) when i can see the file with my own eyes! 当我可以用自己的眼睛看到文件时,这将返回false( 不存在 )!

Anyone had this same issue? 有人遇到过同样的问题吗?

EDIT1: 编辑1:

newFullFileName already contains .png: newFullFileName已经包含.png:

string newFullFileName = string.Format(oldFileName.Substring(0, oldFileName.IndexOf("_") + 1) + DateTime.Now.ToString(), "yyyyMMddhhmmss").Replace(@"/", "").Replace(" ", "").Replace(":", "") + ".png";

As per your edit you are added .png to newFullFileName.. then newFullTempFolderURL will add another .png to your file name. 根据您的编辑,将.png添加到newFullFileName ..中,然后newFullTempFolderURL将在您的文件名中添加另一个.png。

So ti will become FILENAME.png.png it will return wrong. 因此,ti将变成FILENAME.png.png,它将返回错误。

remove .png from newfullFilename and 从newfullFilename中删除.png并

try below 尝试下面

   if (File.Exists( Path.Combine(Global.TempFolder, newFullFileName))
 {

 }

your File.Exists does not contain the file extension as you manually added it when creating "newFullTempFolderURL". 您的File.Exists不包含文件扩展名,因为在创建“ newFullTempFolderURL”时手动添加了文件扩展名。 You need to append ".png" to the File.Exists check or better yet use File.Exists(newFullTempFolderURL); 您需要在File.Exists检查后附加“ .png”,或者最好使用File.Exists(newFullTempFolderURL); as it's already been pre-made. 因为它已经是预制的。

EDIT1: You are adding ".png" a second time. EDIT1:您第二次添加“ .png”。 This is wrong, as the file created is ".png.png", and then you are checking to see if ".png" exists. 这是错误的,因为创建的文件是“ .png.png”,然后您要检查是否存在“ .png”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM