How to handle saved images locally vs on the server - c#

I want to save some generated images.
The part which im confused about is how to handle the paths for saving files.
When running locally (in VS) the images can be saved in c:\images, but on the server id probably want to save them in root-of-webdir\images
do i need a configurable path in the web.config? how can i read the url path for the images in both cases?

You can use Server.MapPath to get the local directory name:
var folder = Server.MapPath("~/images");
This will usually give you 'c:\inetpub\wwwroot\application\images'.

Related

How can I load image into picture box from another computer?

I need my picture box to load image from another computer. It means a different IP address. I have tried loading image from same computer and it works. But I need to load image from a separate server PC. The code below does not work.
pbFeature.Image = Image.FromFile(#"\192.168.232.100\C:\pic\a.png");
Network paths start with \\ not \. Assuming the file is shared correctly you just need pbFeature.Image = Image.FromFile(#"\\192.168.232.100\C:\pic\a.png");
Paste that same path into windows explorer and it should open the image if it is shared correctly.
share the file/folder to access by another computer. then you access file like below
var file= File.ReadAllText(#"\\server\path\filename.png");
then use the file where ever you want

WP8 copy SharedStorge file directly into IsolatedStorage

I am developing a Windows Phone 8 application but am having a lot of issues with file access permission exceptions hindering the approval of my application when ever I try accessing files in the "local" folder (this only happens after the application has been signed by the WP store, not when deployed from Visual Studio). To solve this I have moved all file operations to IsolatedStorage and this seems to have fixed the problems.
I only have one problem left though. My application needs to make use of the file extension system to open external files and this seems to involve the file first being copied to the local folder where after I can then manually copy it into IsolatedStorage. I have no problem in implementing this but it seems that a file access permission exception also occurs once the system tries to copy the external file into the local folder.
The only way I think this can be solved is if I can direct the system to directly copy into IsolatedStorage but I cannot figure how to do this or if it is even possible. It seems as if though the SharedStorageAccessManager can only copy into a StorageFolder instance but I have no idea how to create one that is directed into IsolatedStorage, any ideas?
PS. Do you think that the Microsoft system might be signing my application with some incompetent certificate or something because there is not a hint of trouble when I deploy the application from Visual Studio, it only happens when Microsoft tests it or when I install it from the store using the Beta submission method.
Below is a screenshot of the catched exception being displayed in a messagebox upon trying to open a file from an email:
EDIT:
Just to make it even clearer, I do NOT need assistance in figuring out the normal practice of using a deep link uri to copy an external file into my application directory. I need help in either copying it directly into isolatedstorage or resolving the file access exception.
Listening for a file launch
When your app is launched to handle a particular file type, a deep link URI is used to take the user to your app. Within the URI, the FileTypeAssociation string designates that the source of the URI is a file association and the fileToken parameter contains the file token.
For example, the following code shows a deep link URI from a file association.
/FileTypeAssociation?fileToken=89819279-4fe0-4531-9f57-d633f0949a19
Upon launch, map the incoming deep link URI to an app page that can handle the file
// Get the file token from the URI
// (This is easiest done from a UriMapper that you implement based on UriMapperBase)
// ...
// Get the file name.
string incomingFileName = SharedStorageAccessManager.GetSharedFileName(fileID);
// You will then use the file name you got to copy it into your local folder with
// See: http://msdn.microsoft.com/en-us/library/windowsphone/develop/windows.phone.storage.sharedaccess.sharedstorageaccessmanager.copysharedfileasync(v=vs.105).aspx
SharedStorageAccessManager.CopySharedFileAsync(...)
I've inline the information on how to do this from MSDN http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj206987(v=vs.105).aspx
Read that documentation and it should be clear how to use the APIs as well as how to setup your URI mapper.
Good luck :)
Ok I figured it out. The "install" directory is actually restricted access but for some reason the Visual Studio signing process leaves the app with enough permissions to access this folder. The correct procedure of determining a relative directory is not to use "Directory.GetCurrentDirectory()" but rather to use "ApplicationData.Current.LocalFolder". Hope this helps!

How to create a Text file and save it to a shared-directory?

I'm having an issue here, I developed an application in C# which creates a text file. This text file is saved in the X:\Public\3rd\ASN\, the problem is that in development the files are created and saved with no issues but once I move the application into our Web Server the appplication fails and it throws out this error "Could not find a part of the path X:\Public\3rd\ASN\1175_0001.txt".
This is the code I'm using to saved the file in the directory:
w = File.CreateText("X:\Public\Public\3rd\ASN\1175ASN_0001.txt");
Keep in mind that this directory is another server.
Any help will be really appreciate it.
your X drive is a mapped network drive. You need to use the network url eg \\server\directory\Public\3rd\ASN\1175_0001.txt

How to get path to file in winforms application

How can I get the path of a file on my computer or on the local area network.
I would to show a window that allows me to browse the file system when I click a button, and I want to select a file and get the path to the file. How can I do this?
P.S. I'm not looking to upload the file; I just want to get the path.
The web application is running on the server, and you don't have access to the client file system at all. Can you imagine how much of a vulnerability that would be? I know I don't want the sites I visit inspecting my file system...
EDIT: Question is for a WinForms application
For a WinForms application, you can use the OpenFileDialog, and extract the path with something like this:
If you're looking for the file path:
string path = OpenFileDialog1.FileName; //output = c:\folder\file.txt
If you're looking for the directory path:
string path = Path.GetDirectoryName(OpenFileDialog1.FileName); //output = c:\folder
In general, the System.IO.Path class has a lot of useful features for retrieving and manipulating path information.
To do this in VB.NET use the FolderBrowserDialog.
Due to security restrictions browsers include for user safety, you can't manipulate the client file system directly. You can only use to let them pick a file, and even then it only sends the filename, not the whole path.
The FileSystem API in HTML5 allows for file manipulation, but only within a sandbox for your site specifically, not browsing across the network or other files on the client system.
Instead, provide your users easy steps on how they should use My Computer (or whatever equivalent on other OS's) to navigate to the file and copy & paste the path into a simple text input box.
Have you taken a look at the Path class from System.IO ?

C# Get Full File Path

I have an ASP FileUpload control and I am uploading:
C:\Documents and Settings\abpa\Desktop\TTPublisher\apache-tomcat-6.0.26\webapps\ttpub\WEB-INF\classes\org\gtfs\tmp\GTFS_Rail\routes.txt
What is the C# code to grab this entire string using the code below:
var pathOfCsvFile = Server.MapPath(ImportRoutes.FileName);
var adapter = new GenericParsing.GenericParserAdapter(pathOfCsvFile);
DataTable data = adapter.GetDataTable();
I know that Server.MapPath needs to change.
UPDATE:
Using System.IO.Path.GetFullPath gave me the below output:
pathOfCsvFile = "C:\\Program Files\\Common Files\\Microsoft Shared\\DevServer\\10.0\\routes.txt"
You are mixing up client and server behavior, which is easy to do when you are testing locally. The issue you are having is that the FileUploadControl (and HTML file uploading in general) is specifically designed to not provide you with the full path to the file. That would be a privacy breach. What it is designed to give you is the binary data of the file that was uploaded itself. Specifically you should query the properties on FileUploadControl: FileBytes or FileContent.
Just to further clarify the issue, what would happen if the browser user was actually on a physically different machine from the web server (the usual case)? What good would the full path of the file on the client machine be to you on the server?
Server.MapPath will return the physical path to a file in or below the application root. If that path you list is outside the application root, Server.MapPath will not work.
You can map a virtual directory to a folder you want to use to hold file uploads, which you can then discover with Server.MapPath.

Categories

Resources