I have a rather curious problem.
I have this code (written by someone else). It is suppose to create couple of folders withing each other upon clicking on a specific button. It uses this:
string directoryCrate = "/" + comboIndustry.Text + "/" + comboCustomerName.Text + "/"
Now, as you see, the separator for the folders are "/". But later in the code I need to have the full path of the folder which is c:\projects and then the path to the new folder which is /Industry/Customer/...
So if I want to get the address in the clipboard it would be something like this: C"\projects/Industry/customer/... and obviously I cannot open this address in explorer! For some reason the method won't accept "\" when I try. I am quite confused. Can anyone help?
You can use
System.IO.Path.DirectorySeparatorChar
To get the standard directory separation char for your system. Also, if you try to do a replace, make sure to start the string with an # so it doesn't parse the backslashes or else escaping them as:
string backslash = #"\";
string anotherBackslash = "\\";
Try this:
string path = System.IO.Path.Combine(#"C:\Projects", directoryCrate)
string fullPath = System.IO.Path.GetFullPath(path)
or combine it into one statement.
string fullPath = System.IO.Path.GetFullPath(System.IO.Path.Combine(#"C:\Projects", directoryCrate))
Either of the above two should give you the complete path with backslashes.
Related
How do I go about getting the info from the computers name and setting that location in a string.
Example*
string contents = File.ReadAllText(#"C:\Users\" + Settings.Default.User + "\\Documents\\My vs\\juice.txt");
Problem is, I cannot use the Setting.Default.User location because that name constantly changes.
I need that section to be some kind of variable that means the computers name....
I don't wanna just hard code in my computers name either because If I put this on my other computers then obviously the name would change
you are looking for Environment class:
you can use Environment.MachineName Property
Anyway if you need a special folder path you can use: Environment.GetFolderPath
there you can find all the special folder available Environment.SpecialFolder Enumeration
You can use the Environment.GetFolderPath method, e.g.:
var folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var path = System.IO.Path.Combine(folder, "My vs\\juice.txt");
string contents = File.ReadAllText(path);
You are looking for... Environment.MachineName
You want to use Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) as documented here http://msdn.microsoft.com/en-us/library/system.environment.specialfolder(v=vs.110).aspx
I'm actually working on an app that provides the possibility to the users to upload the files they wish. Those files should also be visualizable once uploaded.
In order to do that I'm trying to get the file path with Server.MapPath and a concatenation of other values. The file path is passed as an argument in a window.open javascript function.
My problem is that I do not get any result at all. No window is opened.
Here is my code:
string completeUrl = Server.MapPath(ConfigurationManager.AppSettings["UsersImagesUploadFolder"] + CurrentUserLogin +
#"\\" + ((GridDataItem) e.Item)["Url"].Text);
string radWindowOpen = "<script type='text/javascript'>window.open('" + completeUrl + "')</" + "script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "fileDisplay", radWindowOpen);
I'm probably missing something obvious but I don't see what it is.
Thank you for your answers.
As Damien has pointed out, Server.MapPath is used for server side path mapping. Clients need to see a path underneath your web app.
For example:
Page.ResolveUrl("~/uploads/" + ConfigurationManager.AppSettings["UsersImagesUploadFolder"] ...
Would resolve a to http://mydomain/vroot/uploads/... etc.
As an aside, note also that #"\\" would result in a double backslash, which I don't think you intended.
Either of #"\" or "\\" would result in a single backslash.
I have url like http://xxx.xxx.xxx/mls/pmmls/12/-8/53/6/12-8536_2.jpg/t1349940727/100x100/
and need to get only filename from url like "12-8536_2.jpg"
url format is dynamic. Filename with extension must be in url. but it filename with extension may not be in last position of url
I have tried Path.GetFileName() but it give "".
is anyone know how extract filename for this type of url?
12-8536_2.jpg does not seem to be a file in that URL. In any case, if the "filename" in the URL will always be in .jpg, you can output the URL to a string (or AS a string) and Regex for it:
string filename = Regex.Match(URL,#"\/([A-Za-z0-9\-._~:?#\[\]#!$%&'()*+,;=]*).jpg").Groups[1].Value
EDIT: I'm thinking this is for a site with different preview sizes for a specific file. You can also specify the different possible extensions as follows (for example):
string filename = Regex.Match(URL,#"\/([A-Za-z0-9\-._~:?#\[\]#!$%&'()*+,;=]*)(.jpg|.JPG|.jpeg|.JPEG)").Groups[1].Value
There is no guarantee that any part of a URL maps to a file, so it does not make sense to try to get the FileName in a URL.
If you know the filename will always be followed by two further segments (in your example, t1349940727 and 100x100), you could do
var input =
"http://xxx.xxx.xxx/mls/pmmls/12/-8/53/6/12-8536_2.jpg/t1349940727/100x100/";
var uri = new Uri(input);
var fileName = uri.Segments[uri.Segments.Length - 3];
If you don't know that, then as others have said, there's no easy way to tell which part is the filename. You could try
var fileName = url.Segments.Last(seg => seg.Contains("."));
to get the last segment with a dot in.
You should define a list of extensions, like .jpg, .png .gif (all files types you are expecting).
Convert your url to string (if it isnt already) and try to find the index of the extension. You do now know the position of the file name and whether there is an file name. remove everything after the file name.
now find a "/" token, and remove the part before (and including) the "/" , repeat this untill you can no longer find "/" (for example with a while function).
more information on how to do this can be found here
static string getFileName(string url) {
string[] arr = url.Split('/');
return arr[arr.Length - 1];
}
I've created a small program wich can read a .txt file.
This file contains a link to another file in this format new_file.txt
The goal is to return the path of the new file, so basically I'm doing this :
String newFileName = getFileName();
int index = oldFilePath.lastIndexOf('\\');
String path = oldFilePath.substring(0, index + 1);
String newFilePath = path + newFileName;
return newFilePath;
For example :
The first file I opened is : C:\a\b\c\oldFile.txt
In this file I found newFile.txt
So the new path will be : C:\a\b\c\newFile.txt
Nice, but what If I find something like this :
..\ or .\.\ or ...
Is there any way to automate this mess ?
Thanks
In C#/.Net you have the rather cool Path class.
You can use Path.GetFullPath( string pathname ) to resolve paths e.g. with \..\ etc in them.
Use Path.GetDirectory(), Path.GetFileName(), Path.GetFileNameWithoutExtension() & Path.GetExtension() to pull names apart and Path.Combine() to put them back together again.
You've tagged this as java as well as c#
In java look at FileNameUtils http://commons.apache.org/io/apidocs/org/apache/commons/io/FilenameUtils.html
The normalize method should help
I am trying to move a file from server \\abc\\C$\\temp\\coll.zip to another server
\\def\\c$\\temp.
I am trying to use File.Copy(source,destination).
But I am getting the error in source path saying: Couldn't find the part of the path.
I am not sure what is wrong with the source path.
You could use a C# # Verbatim and also use checks in the code like this:
string source = #"\\abc\C$\temp\coll.zip";
string destination = #"\\def\c$\temp\coll.zip";
string destDirectory = Path.GetDirectoryName(destination)
if (File.Exists(source) && Directory.Exists(destDirectory)) {
File.Copy(source, destination);
}
else {
// Throw error or alert
}
Make sure that your "\" characters are escaped if you are using C#. You have to double the backslashes or prefix the string literal with #, like this:
string fileName = #"\\abc\C$\temp\coll.zip";
or
string fileName = "\\\\abc\\C$\\temp\\coll.zip";
looks like you need two backslashes at the beginning:
\\abc\C$\temp\coll.zip
\\def\c$\temp
Make sure you are using a valid UNC Path. UNC paths should start with \ not just . You should also consider using System.IO.File.Exists(filename); before attempting the copy so you can avoid the exception altogether and so your app can handle the missing file gracefully.
Hope this helps
It could be the string you are using for the path. If it is exactly as you have entered here I believe you need double backslashes. "\\" before the server name.
I always use network shares for that kind of work, but UNC path's should be available too.
Don't forget that you need to escape your string when you use \'s. Also, UNC paths most of the time start with a double .
Example:
\\MyComputerName\C$\temp\temp.zip
Actually I missed # before the two strings.The source and the destination path.
That is why it was giving error.