To check whether image file exist or not - c#

string serverPath = HttpContext.Current.Server.MapPath("~/web" + event.Image_Url);
var isFileExist = File.Exists(serverPath);
The value of event.Image_Url = /Resources/images/event-images/e1ae04a2-e63f-4831-a5ee-2f0d2713f8a2.png
But it always gives false even though the file exist on the physical path.The physical path which it comes from above operation as shown below.
serverPath value = D:\Freelance Work\Trunck\Api\web\Resources\images\event-images\e1ae04a2-e63f-4831-a5ee-2f0d2713f8a2.png
But actually I need to go to the web folder.But it automatically gets the Api folder as shown above.How to avoid it ? Why it takes the Api folder ? Any help would be highly appreciated.
Note : I have noticed that the web api project is also running on local host.May be that is the reason for it.But how can I tell it to get the virtual path from the web project ?
Folder structure within Trunck as follows.All are in same level.
Trunck --> API
--> Web
--> BLL

The problem was the / at the start of the event.Image_Url field. That caused the last path in the string to be taken as a absolute path.
string serverPath = Path.Combine
( HttpContext.Current.Server.MapPath("~")
, #"..\web\"
, #event.Image_Url.TrimStart('/').Replace('/', '\\')
);

I think you need to try string path= Server.MapPath("~/web"), serverPath =Path.Combine(path,event.Image_Url) instead of string serverPath = HttpContext.Current.Server.MapPath("~/web" + event.Image_Url);

Related

Image folder creation in web api using concatenate strings

I am new to c# ,Here I'm trying to form a URL to store the image in API itself .
I want to create a folder in the following structure
Images_Folder --> Fruits_Folder--> Seedless_Folder --> Image.jpg
coding :
string imageURL = HttpContext.Current.Server.MapPath("~/Images/");
folderPath = imageURL + formData.RootFolder + formData.TypeFolder + "/";
filePath = (folderPath + postedFile.FileName);
postedFile.SaveAs(filePath);
By using the above code it stores the image in the following structure.
Images-->Fruits_FolderSeedless_Folder --> Image.jpg
While debugging the code I could see the URL format as follows
"D:\Projects\Dot Net\FruitsDisplay\FruitsDisplaySolution\Images\FruitsSeedless/"
Ex: Images-->FruitsSeedless-->Image.jpg
But I want it should be as Images-->Fruits-->Seedless-->Image.jpg
can anyone help me to solve this.
Let OS decide what to use for sub directories, as it might not always be the familiar \ character. Using Path.Combine() method uses the character that is valid in that environment:
folderPath =Path.Combine(imageURL,formData.RootFolder, formData.TypeFolder, postedFile.FileName);

Storing MSSQL LocalDB for Application

I have been using a LocalDB.mdf file to build my application, but now need to use it in production and create an installable application.
After research, it seems the best place to store such a file is in the /Appdata/Local/ folder.
I intend to create a new LocalDB.mdf file in there if it doesnt already exist, or has been deleted.
I have a pre-made LocalDB.mdf file in my App Resources, which I wanted to copy into this /Appdata/Local/ folder on first run, but I am seeing an Access is denied error.
I can create a blank file ok to that folder.
Here is the code:
string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string dvAppDataFolder = appDataFolder + #"\MyApp";
AppDomain.CurrentDomain.SetData("DataDirectory", dvAppDataFolder);
if (!Directory.Exists(dvAppDataFolder))
Directory.CreateDirectory(dvAppDataFolder);
if (!File.Exists(dvAppDataFolder + "LocalDB.mdf"))
{
File.WriteAllBytes(dvAppDataFolder, LokiServer.Properties.Resources.newDB);
}
In addition, Am I going about this the right way?
This line
if (!File.Exists(dvAppDataFolder + "LocalDB.mdf"))
is probably wrong. Missing the backslash, better to use Path.Combine instead of a string concatenation.
Finally, you want to write to a file not to a folder
string fileName = Path.Combine(dvAppDataFolder,"LocalDB.mdf");
if (!File.Exists(fileName))
{
File.WriteAllBytes(fileName, LokiServer.Properties.Resources.newDB);
}
Are you doing it right? It depends. If your app data should be kept separated for each user of your target machine then you are right, but if you want your database to be available to all users of that machine then use
string appDataFolder = Environment.GetFolderPath
(Environment.SpecialFolder.CommonApplicationData);

how to make a setup of c#.net project with relative image path

I am using avatar photos in my application , in my app I created an Image folder in which I kept those pics. And as a path used relative ie;
images\gravatar.png
Also , made Copy to Output Directory - Copy to
But when I am making a setup and installing it on client machines its not able to find the path and give Unhandled Exception error on the path.
I tried researching only thing I got was how to use in development not in Deployment.
Thanks
If you want to use a relative path, set a constant for where the executable started from. Then the paths can be relative to that one.
public static readonly string APPLICATION_ROOT_PATH = Application.StartupPath + "\\";
public static readonly string IMAGES_PATH = APPLICATION_ROOT_PATH + "\\images";
Now you can use those constants like any other path. For example:
m_openFileDlg.InitialDirectory = className.IMAGES_PATH;

Converting a local folder structure to a folder structure on a server

I'm currently playing around with Paths in .Net and have run into some difficultly with regards to replicating a local folder structure passed as a string from a web service to my site running under localhost on IIS Express.
Essentially, our users will select an image within our desktop software, the local path of which will be sent as a property of the image in our web service. So, when my script accesses the web service, it is fed a string such as:
C:\\Users\\axumadmin\\Pictures\\axumImages\\Countries\\Canada\\canadianFlag.jpg
What our users will then do is FTP this folder structure to a specified directory on our server:
ServerRoot\\umbraco\\axumImages\\Countries\\Canada\\canadianFlag.jpg
The main issue I have here is that I cannot seem to modify the path retreived from the web service to only return the directories from axumImages downwards. So in essence, my local path would be converted to:
axumImages\\Countries\\Canada\\canadianFlag.jpg
I have already tried playing with System.IO.Path to convert this path into the format that I wish to be returned but ultimately all I have acheived so far is either retreiving just the image filename:
canadianFlag.jpg
System.IO.Path.GetFileName(image.FileName);
or the parent directory of the image
C:\\Users\\axumadmin\\Pictures\\axumImages\\Countries\\Canada
Therefore my question is, how can I parse the string so that it is only using axumImages and its descendants?
Any help would be greatly appreciated.
use string.Substring
var startIndex = image.FileName.IndexOf("axumImages");
string test = image.FileName.Substring(startIndex, image.FileName.Length-startIndex)
I ended up solving this in the end using string parsing.
string test = image.FileName.Split(new[] { "axumImages" }, StringSplitOptions.None)[1];
In this example, image.Filename is my filename. So if the filename is:
C:\\Users\\axumadmin\\Pictures\\axumImages\\Countries\\Canada\\canadianFlag.jpg
this will return
\\Countries\\Canada\\canadianFlag.jpg
I can then concatenate this into a usable variable below which then amounts to the path I require:
var actualPath = "axumImages" + "test";

File.Delete isn't working to delete image from sub folder

In our ASP.NET program a user can upload an image to a folder. The location of the image (including the name of the upload folder which is in the root directory) is stored as a variable called "path", ie. "Uploads/fileName.jpg".
To remove the image:
if (File.Exists("~/" + path))
{
File.Delete("~/" + path);
}
However, it fails to run because it can't verify that the file exists. Through some testing we noticed it's looking for "path" in the "system32" directory. Why would this be?
You need to use Server.Map path to ensure that the Tilde is resolved correctly.
MSDN Article is here -> http://msdn.microsoft.com/en-us/library/ms524632(v=vs.90).aspx
Your code would become
var fixedPath = Server.MapPath("~/" + path);
if (File.Exists(fixedPath))
{
File.Delete(fixedPath);
}
The File class is not aware of the IIS directory mapping, so it won't understand ~ correctly. You have to first use a method to map the app relative path to a local path with Server.MapPath

Categories

Resources