How to convert Relative Path into absolute path in c# - asp.net-mvc

I have stored Database in the main folder of my project, I am using Relative Path while i use that database. Now i need to convert this realtive path into absolute path at runtime
I used tha following code but it doesnt work
string Path1 = #"Data Source=|DataDirectory|\MakeMyBill.sdf";
string fullpath=Path.GetFullPath(Path1);

You can do:
String absolutePath = Server.MapPath(myRelativePath);

try like HostingEnvironment
string logDirectory = HostingEnvironment.MapPath("~") + "\\" + "App_Data\\MakeMyBill.sdf";
or
string logDirectory =Server.MapPath("~/App_Data/MakeMyBill.sdf")
or on any folder
string filePath = #"D:\file\";
string directoryName = Path.GetDirectoryName(filePath);
filePath = directoryName + #"\file.xml";

Related

How to split this string in Dart? /data/data/com.example.trail/cache/IMG_1645484057312.png

I need to get the name of an image path, which is a String. How could i say programmatically in dart "when you find the first / from the right hand side split it, then give it to me"?
the string which i need to split is:
'/data/data/com.example.trail/cache/IMG_1645484057312.png'
You can use split like the #scott-deagan answer for it. But if you intend to support cross-platform path manipulation, you need to use path package.
Example:
import 'package:path/path.dart' as p;
void main() {
var filepath = '/data/data/com.example.trail/cache/IMG_1645484057312.png';
print(p.basename(filepath));
print(p.basenameWithoutExtension(filepath));
}
result:
IMG_1645484057312.png
IMG_1645484057312
void main() {
var someFile = '/data/data/com.example.trail/cache/IMG_1645484057312.png';
var fname = someFile.split('/').last;
var path = someFile.replaceAll("/$fname", '');
print(fname);
print(path);
}
Here is the way I recommend you to test
First, do the split on the original string by "/" splitter, then extract the last member of the list created by the splitter to get the name of the png file.
Second, for extracting the remaining string (i.e. the file path), use the substring method of the class string. just by subtracting the original string length from the last_member length in the previous portion, you are able to get the file path string.
Hope to be useful
Bests
void main() {
String a = '/data/data/com.example.trail/cache/IMG_1645484057312.png';
var splited_a = a.split('/');
var last_image_index = splited_a.last;
String remaining_string = a.substring(0, a.length - last_image_index.length);
print(remaining_string);
print(last_image_index);
}
result:
the result of path and file extraction from a string in dart

Separating Filename from Filepath String

I'm separating a filepath string into two separate strings:
filepath with just directories (filename removed)
filename
I'd like to work within a CPS context, which prevents some closure usage. I've got the following working code, but I'm unsure if it is good practice. The filepath references an "ID Version" file named 'idver.h.' 'projectPaths' is a map containing the filepath.
//pull off the filename from end of filepath
String[] idVerPathArray = projectPaths.idVerPath.split('/')
String filename = idVerPathArray.last()
//get subarray without last element
String[] idVerJustPathArray = Arrays.copyOfRange(idVerPathArray, 0, idVerPathArray.length - 1)
String idVerJustPath = idVerJustPathArray.join('/')
def file = new File(projectPaths.idVerPath)
String filename = file.name
String idVerJustPath = file.parent

While converting NSString to String my result changes

I request you to give me a solution where the result of NSString and String both are same
downloadedFile = MOM'&^*%s-HC.pdf (In NSString)
let path = pathComponent.appendingPathComponent(downloadedFile as String)
print(path) //OutPut: ttt_gmail_com/MOM'&%5E*%25s-HC-2.pdf
If I test with regular expression result is OK.
let NSStringValue = "MOM'&^*%s-HC.pdf" as NSString
print(NSStringValue) // Output: MOM'&^*%s-HC.pdf
let StringValue = downloadedFile as String
print(StringValue) // Output: MOM'&^*%s-HC.pdf
but while I put that code in appendingPathComponent it changes my result.
You have a file name which has all the special characters here, I tried your case in a sample project trying to load such file in UIDocumentInteractionController and it seems when I have a file name with special character in my document directory and i try to retrieve it, few special characters are URL encoded.
Observe the file name I used here is "MOM'&^*%s-HC.pdf" and saved it in the doc directory but when I retrieve it from the document directory using the following code few characters are URL encoded as I am fetching the path as URL.
So this means that your original file name
MOM'&^*%s-HC.pdf
is now alterated and replaced by percentage escape sequence strings,
MOM'&%5E*%25s-HC.pdf
observe the below code which I used
var pdfURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
pdfURL = pdfURL.appendingPathComponent(fileName)
To resolve this all i did was removed the percentage escape sequence when I am creating the URL and it worked I was able to view the PDF in the UIDocumentInteractionController
fileName.removingPercentEncoding!
Hope it helps.

Insert variable | Data base | AT commands | SIM900

I trying to send data temperature from Arduino to data base... I've finished the connection, but I need replace a part of String, that is the static URL:
SIM900.println("AT+HTTPPARA=\"URL\",\"http://mail.interseccion.com.mx:8901/dbTemperatura?Id_temp=0&Id_Device=1&Valor=-127.7&Temperatura_Action=Insert\"");
and this is my variable:
float = tmp;
tmp = sensor.getTempCByIndex(0);
And the URL I need replace the "-127.7" for the variable... but remember, the URL it's a String. I hope you can help me, thanks!
I don't use Arduino very much, but maybe this could help:
http://playground.arduino.cc/Main/FloatToString
https://www.arduino.cc/en/Tutorial/StringAdditionOperator
I got the solution...
This is my URL
SIM900.println("AT+HTTPPARA=\"URL\",\"http://mail.interseccion.com.mx:8901/dbTemperatura?Id_temp=0&Id_Device=1&Valor=-127.7&Temperatura_Action=Insert\"");
and the parameter to replace is the "-127.7"
I divided the URL on two parts into Strings...
String stringvar = String(tmp);
String stringurl1 = String("AT+HTTPPARA=\"URL\",\"http://mail.interseccion.com.mx:8901/dbTemperatura?Id_temp=0&Id_Device=1&Valor=);
String stringurl2 = String("&Temperatura_Action=Insert\"");
String urlfinal = String(String(url1) + String(strinvar) + String(stringurl2));
For anyone has the same kind url...

How do I encode a complete URL string in ASP MVC

I get a url string and would like to transform it to a legal http url:
For example:
"http://one/two/three%four/five#five?six seven" should turn into "http://one/two/three%25four/five%23five?six%20seven"
However, HttpUtility.UrlEncode does not help, as it encodes the entire string (including the legal "://").
See if this what you want?
Uri uri = new Uri("http://one/two/three%four/#five?six seven");
string url = uri.AbsoluteUri + uri.Fragment;
// url will be "http://one/two/three%25four/#five?six%20seven#five?six%20seven"
How about splitting and rejoining:
string url = "http://one/two/three%four/#five?six seven";
string encodedUrl = "http://" + string.Join("/", url.Substring(7).Split('/').Select(part => HttpUtility.UrlEncode(part)).ToArray());

Resources