Click here to Skip to main content
15,886,685 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I need to convert something like
c:\\test\\file1.txt
to url
\\localhost\\c:\\test\\file1.txt
Posted
Updated 30-May-22 23:00pm
v2

From what I understand of web hosting, that is not the way it works at all. Depending on what server you're using and what the server settings are, localhost is actually pointing to a specific folder on your system. You can only access files located within that folder.

I could be wrong, but since no one else had replied and that is how I understand it, I thought I would throw that out there.

You can do things with PHP using include_path to be able to access files outside. And with Apache, you can do things like Aliasing (see Mapping URLs to Filesystem Locations[^]) and I assume you can do the same with other servers. But, you have to tell the server those types of things. Otherwise, any person could type what you wanted to and create all kinds of havoc.
 
Share this answer
 
If this has been for ASP.Net and vice-versa you could have used Server.MapPath but this way, I think you can brute-force it :)

C#
string serverName = "localhost";
string filePath = "c:\\test\\file1.txt";
string result = string.Format("\\{0}\\{1}",
    serverName, filePath.Replace(":", "$"));


I think in case of url you have to replace : with $ too (if you want to use it with networks or so).
 
Share this answer
 
saeidx wrote:
I need to convert something like c:\\test\\file1.txt to url
\\localhost\\c:\\test\\file1.txt


I don't know, it looks impossible to me.
 
Share this answer
 
It depends on where this program will be run.
1. On the same computer as the file is located.
In this case your original string will work just fine.

2. On another computer that is on the same network as the computer with the file on it.
In this case you will need to do a few non-programming things to set up you system first.
First you will have to set up a 'shared' folder on the computer that has the file and make sure that whoever will need to access the file (whoever will be running the program) has access to the folder.
In this case I would suggest making 'test' your shared folder. Do not make 'C:' your shared folder or else you will regret it. When you make it a shared folder you will have to give it a name, 'Test'; seems fine.
After setting the properties of the folder you will need to figure out the name of the computer that the file is on.
For now we will just call it 'MyRig'. Once this is done, the file can be accessed with the following URL '\\MyRig\\Test\\file1.txt'
If you had named the share folder 'MyTestFiles' the path would be '\\MyRig\\MyTestFiles\\file1.txt', even though the actual folder name is 'test'.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900