How to display Image from outside project directory - c#

I have an Image Path which is of D: drive and I want to display that Image on my Image Control of asp.net.So, how to provide file path to Image.ImageUrl?
"D:/Folder/001_001.jpg"
I tried with server.MapPath() method but doesn't work.

If you're deploying in IIS, you could create a virtual directory Images that would point towards D:/Folder, then you could set your ImageUrl to ~/Images/001_001.jpg.jpg.
An alternative solution could be to create a symlink between the two folders. You would run something like the following in cmd mklink /D ""D:/Folder" "[YouProjectPath]/Images", then set your ImageUrl to ~/Images/001_001.jpg.jpg. I would stay away from this though as it would only work on a machine where the project lives in the exact same path as yours.

You can make local path as following "file:///D:/Folder/001_001.jpg".
However, it's strongly not recommended to use this practice even if your site is planned to stay on your local computer only and will never been deployed to real Internet. You should copy / upload the file to your site's subdirectory 'Images' and then use the relative path '~/Images/001_001.jpg'.

Related

File path for c# when moved to a memory stick

sorry first time user and currently learning c# at uni, i'm working on an assignment and i'm trying to get the file path to work on the memory stick as that's what i need to hand it in on, thanks many regards
I'm not completely sure what your question is by I think what you're talking about is absolute vs relative paths. If you use an absolute path like "C:\users\yourname\blalba\project\stuff", then it's obviously only going to only work on your computer. However, you mostly all of the time want to use relative paths. Relative paths have the root directory of the build output files for your project; where your .exe file is built for your project. This is usually in "projectdir\bin\debug" or "projectdir\bin\release". So if you put for example a file called 'test.txt' in that directory, you can simply put the relative path "test.txt" instead of "C:\users\yourname\blalba\project\bin\debug\test.txt". If you were to put 'test.txt' in the project directory, you can use the relative path "....\test.txt". "..\" means navigating one step back.
The path to the directory containing the files you are looking for is "F:\Mod005244, 1715840". The "1715840 JH" is just the name of the drive. You access different drives via the drive letter. In this case, the drive letter "F" was assigned to your flash drive.
I would suggest making the file path configurable or request input from the user of the program as the drive letter of a flash drive will not always be "F". There are even classes (such as FileBrowserDialog) that will open a graphical file browser dialog and prompt the user to navigate and select a file.

copy a file to shared folder on another computer

I am trying copy file to mapped network location.
If I try to do it manually everything is working OK.
By running following code I don't get any exceptions but I not get the code at the needed location.
string _sharedLocation = #"C:\Users\pddd\AppData\Roaming\Microsoft\Windows\Network Shortcuts\system-tests";
if (Directory.Exists(_sharedLocation) && File.Exists(#"c:\\Automation\\Tests\\Test1\\events.json"))
{
File.Copy(#"c:\\Automation\\Tests\\Test1\\events.json", Path.Combine(_sharedLocation, "events11.json"), true);
}
Any suggestions with that issue.
looking at the _SharedLocation variable, it's on location: "...\Windows\Network Shortcuts\..."
I'm just guessing here, but are you tring to refer to a shortcut to a network folder, rather than a network folder?
This will never work:
File.Copy(myOriginalFile, "C:\...\MyShortcutToANetworkFolder\myFile.txt");
Why not? Because a shortcut is basically a file, not a folder (it's more complicated than that, but I'm keeping it simple for argument's sake). You cannot put a file (or anything else) into a shortcut. The only thing you can do with a shortcut is open it.
You need the actual network folder path.
This will work:
File.Copy(myOriginalFile, "\\myServer\myFolder1\myFolder2\myFile.txt");
It seems that the target path _sharedLocation also refers to a local path, not a remote path.
I guess you sharedLocation path is not valid.
If you write #"c:\" it will refer your local drive on which the code is running
so Please correct it
Problem: Your shared Path refers to C: drive of same machine. Possibly you are referring to the shortcut of mapped network location.
string _sharedLocation = #"C:\Users\pddd\AppData\Roaming\Microsoft\Windows\Network Shortcuts\system-tests";
It should be:
string _sharedLocation = #"\\ComputerNetworkIdentity\SharedFolder\pddd\AppData\Roaming\Microsoft\Windows\Network Shortcuts\system-tests";
Shared computer can be located using \\ComputerName.
You must have Write permission on shared folder.
Simple Way Locate A Shared Folder:
Open Run dialog.
Type computer name with preceding backslash e.g. \\ComputerNetworIdentity
Locate the folders shared by the network computer.

How to Check URL Path Of Images

Suppose I have an Image located at “C:\A\mah” Or “E:\A\mah” my question is can I check the URL Path of such an image? If yes then how? Which will lots help me to put right URL path in my Asp.Net Web Application.
If those images are not within your project folder, you may need to create a virtual folder (say myImages) and then access images.
#Url.Content("~/myImages/imgName.ext")
Otherwise you could use
#Url.Content("~/path_relative_to_main_folder/imgName.ext")
Never use exact locations. It'll give you a headache. Instead, put a folder in your site's directory somewhere and then reference it with relative paths. For example:
<img src="#Url.Content("~/Images/ABC.png")" />
This means it is looking for the Images folder in your project's root directory.

Problem in displaying image in the web page

Trying to display an image in the webpage tried like below.
Image1.ImageUrl = "C:\\Users\\naresh\\documents\\visual studio 2010\\Projects\\Vacancy\\Vacancy\\Files\\3\\710d7a38-5a4d-44fc-883f-5996150ba507.jpg";
Image2.ImageUrl = "C:\\Users\\naresh\\Desktop\\710d7a38-5a4d-44fc-883f-5996150ba507.jpg";
Second one Worked but first one didn't. I think the problem is with the spaces in the path. How can I resolve this problem?
Why do you not use a relative path to the location of your site? The path will be shorter and when you will port the web site on production server the image will still work (if the image is port to the server)
May be with the Vacancy which you have given two times. Check it, if its the mistake.
You can try to replace the spaces with %20.
But I wonder if that's the problem because the image is located at your local disk. Is this webpage only running on your local machine? Does the image exist?
Put quoted quotation marks into the string, e.g.:
Image1.ImageUrl = "\"C:\\Users\\naresh\\documents\\visual studio 2010\\Projects\\Vacancy\\Vacancy\\Files\\3\\710d7a38-5a4d-44fc-883f-5996150ba507.jpg\"";
Try giving the virtual directory path (create a virtual directory for the image folder. If it is in the root itself use the http://systemName/virtualDirectoryName/ImageFolder/image1.jpg) instead of physical path as follows:
Image1.ImageUrl=http://systemName/virtualDirectoryName/ImageFolder/image1.jpg
Hope this helps..
You are doing anyway very bad things in there. You should use Server.MapPath to resolve dynamically an IIS relative path into the file system path and you should not, never ever, load an image from the user's folders.
Put the image inside a subfolder in your web application, like for example create a folder called images at the same level where your aspx pages are.

Quick Question: How can I make sure the current directory is not changed because of OpenFileDialog?

say my application is installed in C:\programfiles\xyz\ I've some setting & other data files (*.dat files) in this directory too.
My application uses OpenFileDialog to open an image. Problem is that when ever user browses to some directory (say MyPictures) for an image. The current working directory of the application becomes that directory (MyPictures in this case).
After user inputs the image. I do some processing over it and save some values to imagedata.dat which will be located in the path where original application is installed.(C:\programfiles\xyz here )
In my code I'm just using FileStream("imagedata.dat",...,...) without any path prefix before the filename. Problem here is that application is crashing because it is searching for imagedata.dat in 'MyPictures\imagedata.dat'.
How can I avoid this?
You should be using absolute path names when saving data to files. The current working directory is controlled by the user, not by you (for example, if they launch your process from a shortcut then the working directory could've been changed before your process even starts up).
Also, you should never save anything under C:\Program Files during normal use. Doing this means your program needs to be running as an administrator, and unless you're doing administrator-y things then you should be able to run it as a regular user.
The correct thing to do in your case is to use the Environment.GetFolderPath() function to get the location of the ApplicationData folder and save your data under there. Just choose a sub-directory based on your application's name.
You could save it to GetCurrentDirectory then restore with SetCurrentDirectory. However, I agree wih codeka that using the appropriate GetFolderPath (probably ApplicationData, CommonApplicationData or LocalApplicationData) is a better solution.

Categories