Format a hyperlink to open the C drive - c#

I am showing a list of hyperlinks on a web page that are intended to open windows explorer at the folder containing the file. The file paths are stored in a database and retrieved.
If the file path in question is showing in my C# code as \\\\myserver\\folder1\\somedocument.doc
I can set the href of the hyperlink to be: file://\\\\myserver\\folder1 and it works. Windows Explorer opens and shows the contents of folder1.
But, if the user who originally specified the file, selected one on his C:\ drive, I might have a path like this to deal with C:\\Somefile.txt
In this case I want to format the hyperlink so that it opens Windows Explorer and shows the contents of the C: drive. How can I do this?

file:///C:/ would work. But note, that this is only working in Internet Explorer. Neither Chrome, nor Firefox support such a behaviour. (Chrome and Firefox will list the drive's content in the browser instead of opening Windows-Explorer)
There are extensions for chrome, but i dont think, that this is what you are looking for.

It should work just the same as a network path. Have you tried file://c:\?
Other users will not be able to see that file of course, only the one that submitted it.

file:///c:/ should work for this

To open C:\ by default you can use: "explorer.exe /e,C:\"
Same syntax applies to fileshare: "explorer.exe /e,\myserver\folder1"

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

How to create a (cross browser compatible) hyperlink that opens a file on a network drive?

I want to open Documents from Shared Folder on network using ASP.NET with C#.
My situation as follow:
My Web Server Name is “WebServer1” use IIS 7
My DC Server Name is “DC1”
My File Server Name “FileServer1”
My Files Path
\\FileServer1\A\B\C\MyDoc.docx and \\FileServer1\A\B\C\MyPDF.pdf
I tried the following
1- I Created Hyper Link
link to file
The files open on some machines that uses Internet Explorer version 8 but on any other version or Chrome, Fire Fox, or any other web browser it doesn't work when I click on the hyper link no action happens and no errors, but if I copied the link address and pasted it into the web browser the file opens
I gave the path in the file server full control permission for “everyone”
2- I also tried to create a button with the following code
Response.ContentType =
"application/vnd.openxmlformats- officedocument.wordprocessingml.document";
Response.AppendHeader("Content-Disposition", "attachment; filename=5401-1-2289.docx");
Response.Redirect("file:///// FileServer1\A\B\C\MyDoc.docx");
Response.Flush();
First of all, the URL is incorrect. It should be #"file://\\FileServer1\A\B\C\MyDoc.docx" instead of "file:///// FileServer1\A\B\C\MyDoc.docx" (I have a hard time thinking this actually compiles, because you didn't escape \...)
Second, you are combining both sending content, as redirecting, as pointed out by Luaan in a comment. This won't work.
You could transmit the file directly. This also prevents you to expose your network drive to external users:
Response.TransmitFile(#"\\FileServer1\A\B\C\MyDoc.docx");

How to open html file with anchor(#) in C# with default browser

I am trying to open a contextual help file in c#.
When i specify no anchor, it works perfectly.
Process.Start("C:/Help/Help.htm")
But when i specify anchor, it does not open
Process.Start("C:/Help/Help.htm#_Toc342057538")
Internally it changes '#' to '%23' and the path becomes "c:\Help.htm%23_Toc342057538" which browser is unable to recognize.
Browser is successfully opening the path "c:\Help.htm#_Toc342057538"
How to stop this automatic conversion by Process.Start. The same behavior is observed, if i give the anchor label as another argument, or use Uri class.
EDIT
Same behavior is observed, when i enter the string in Window Run. Following command also convert # to %23, which browser cannot recognize.
chrome c:/Help.htm#_Toc342057538
On my Windows 7 system, both of the following open C:\Help\Help.htm in Internet Explorer and scroll to the _Toc342057538 anchor:
Process.Start("iexplore", "file://C:/Help/Help.htm#_Toc342057538");
Process.Start("iexplore", #"C:\Help\Help.htm#_Toc342057538");
For Firefox and Chrome, only the file protocol seems to work:
Process.Start("firefox", "file://C:/Help/Help.htm#_Toc342057538");
Process.Start("chrome", "file://C:/Help/Help.htm#_Toc342057538");
Try this out. I just did it myself and working in internet explorer
string s = "file:///D:/tmp/test.html%23test";
s = uri.UnescapeDataString(s);
Process.Start(s);
Please let me know if it is working or not for you.

Opening up PDFs and other documents from Silverlight out of browser

I'm having a little problem figuring out the best way to open up a file that I have stored in a database. The file is being stored as a byte array in a nvarbinary field in the database. Currently when I want to open up a file I use an ASP.NET webpage that I pass a variable to and write the file stream to the page. This works fine when using the in browser version of the Silverlight application, but when out of browser I cannot invoke a browser window to open because I don't have access to dom.
How can I open the bytearray from Silvelright without invoking a browser window? I'm able to pass the bytearray and file type to the Silverlight app no problem. I just don't know how to display it once I have it there..
Thanks!
If you are targeting windows (with full trust enabled, and not mac), you can do this out-of-browser by first writing the file to disk (either in isolated storage or in My Documents), then using a WScript.Shell COM object to have the OS open the file.
After you have saved the byte stream to a file and have the file location, you can do:
using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell"))
{
shell.Run(fileLocation); //works similar to start -> run -> filename
}
If you want to leverage your existing ASP page, you can pass its URL to shell.Run and the OS will use the user's default browser to open that page.
On a mac, the best you could do is save the file to their user directory, and have them manually navigate there with finder and double-click it.

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 ?

Categories

Resources