This question already has answers here:
Relative path to absolute path in C#?
(8 answers)
Closed 8 years ago.
My main function takes arguments from command line.
This arguments looks like
--args ../../TempImages ../Resources.csproj etc.
How can i find full path to this folder in c# from this arguments?
Like
Users/%username%/Projects/Resource/Resource.csproj
I tried use Path, Directory, Enviroment classes, but nothing helps me
I am using Xamarin, MacOSX 10.9
EDIT:
Forget to say, that i don't know fullpaths as provided above. My script are running on different systems. So, fullpath to these files can be different.
Moriarty's answer helps me, but with this condition, it doesn't work.
Luckily Mono has adopted this quite well in the System.IO space. You're in the right direction with the Environment classes, like so:
var path = Path.Combine (
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
"Projects",
"Resource",
"Resource.csproj");
The Environment.GetFolderPath, in combination with Environment.SpecialFolder.UserProfile, will do the trick to fetch what is known in Windows as %USERPROFILE%.
Second, notice I've split the /Projects/Resource/ part in seperate arguments for the Path.Combine function. This is to prevent issues with Windows<>Mac<>Linux file operations and let mono figure this out for you.
So this'll work on all platforms. Enjoy!
Update:
For the downvoters that consider this to be a wrong answer: the suggestion mentioned in the referenced duplicate article does not work on Mac or Linux. This is because "/paths/with/slahes" act differently on Mono/Xamarin platform in non-Windows environments. And this is what the original question was about. #imho
Related
This question already has answers here:
c# why when the path is "C:" the directoryInfo takes me to the application folder?
(4 answers)
Closed 5 years ago.
In a C# program I am creating an instance of DirectoryInfo. Normally it does not seem to require a trailing slash after a directory name. But if I pass in "C:", rather than getting the root directory for my hard drive I get the directory where my executable is! This certainly seems like a bug but is there some hidden behavior that I am missing?
It isn't explicitly called out in the documentation, but using just (drive): isn't listed as a valid path specification among those that are listed.
The behavior you are seeing is as implemented though, as you can see from the .NET sources:
http://referencesource.microsoft.com/#mscorlib/system/io/directoryinfo.cs,90
The Init method (called from the constructor) does a check for this case, and if it finds it, uses the current working directory (".") instead. Depending on how you launched the EXE, the current working directory could be the location of the EXE.
This is probably a rudimentary question but I am still kinda new to programming and I've wondered for awhile. I've done multiple projects in Python, C#, and Java, and when I try to use new libraries (especially for Python) people always say to make sure its in the right PATH and such. I just followed an online tutorial on how to install Java on a new computer and it rekindled my question of what a path really is. Is the Path just were the programming language looks for a library in the file system? I get kinda confused on what it's significance is. Again, I'm sorry for the wide question, its just something that I've never quite gotten on my own programming.
EDIT: I just wanted to thank everyone so much for answering my question. I know it was a pretty dumb one now that I've finally figured out what it is, but it really helped me. I'm slowly working through as many C#, Java and Python tutorials as I can find online, and it's nice to know I have somewhere to ask questions :)
The PATH is an environment variable which the shell (or other command interpreter) uses to search for commands. Usually (always?) commands are found with a greedy algorithm, so entries that come first in the PATH are returned first. For example, a command in /usr/local/bin will override a command in /usr/bin given a PATH such as
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
while the purpose is consistent, the syntax is slightly different on WINDOWS - you would use
C:\> ECHO %PATH%
to "echo" your PATH.
First my shell is going to search /usr/local/sbin then /usr/local/bin then /usr/sbin and then /usr/bin before searching /sbin and /bin if the command isn't found then it will report that it couldn't find such a command...
# Like so
$ thisprogramdoesntexist
thisprogramdoesntexist: command not found
Now, on Linux at least, there's also a LD_LIBRARY_PATH which the system will use to search for dynamic libraries (greedily), on Windows I think it just uses the PATH. Finally, Java uses a CLASSPATH which is similar (but used to search for classes and JARs).
On Linux one might add an entry to the PATH like so,
$ export PATH="$PATH:/addNewFolder"
While on Windows you might use
set PATH=%PATH%;c:\addNewFolder
Sometimes, you might manipulate your PATH(s) to enable specific functionality, see update-java-alternatives on Ubuntu for an example.
A PATH is a file directory on your computer. If you need to install a programming language, you might need to put it in your system PATH variable. This means that the system looks to these files for different information, IE where the libraries for the code you are using are.
Hope that helped!
Exactly as other said, PATH is a list of folders that is included in the search -other than the current folder- and you can always access straight away. It's one of the Environment Variables.
For example, we have the python folder in C:\Python27. I'm sure you know that to run a python file, we commonly use python script.py.
What happens is that the command line searches for python.exe in your current folder, and if not found, search it in the folders in the path variable.
To read the path, you can, straightforwardly use:
$ PATH
If you're on windows, like i am, an easy way to deal with this is to just use System Properties. Just type it in the start menu, open it, and go to the 'advanced' tab. Click on the Environment Variables, there! You'll see a PATH variable, and you can modify it as you want.
I myself use more than one version of Python, and to deal with this, i appended all the folders to PATH, and changed my python.exe to pythonversion_number.exe. Problem solved! Now, i can run this in the command line:
$ python26 script.py
$ python33 script2.py
Some further reading on this, if you're interested, here's a good question asked
Hope this helps!
The best resource (so far) about PATH information, you can see in this question:
https://superuser.com/questions/284342/what-are-path-and-other-environment-variables-and-how-can-i-set-or-use-them
Stack Overflow is not the best place to search about this, always check the amazing
https://superuser.com/ for this kind of question.
PATH is a symbolic name usually associated to string values separated by a semicolons (where each string part is a directory name). This symbolic name (and its values) is handled by the operating system and could be modified by the end user through the some command line instruction like SET PATH=........ or through some kind of user interface configuration tool.
It is common practice for tools like compilers or other programming tools to look at this symbolic name and use the list of string values for searching files that are not directly available in the current folder used by the tools.
So, if an installation procedure set the PATH symbol in this way
SET PATH=%path%;C:\PROGRAM FILES\MYTOOLFOLDER;
it means, set the PATH symbol to the previous value (%PATH%) and add another string value to it (C:\PROGRAM FILES\MYTOOLFOLDER).
Then the tool, when it needs to search for a particular file or library, could read the PATH symbol values, split them at the semicolons and iteratively look at the directories listed one by one looking for the file required.
In C# programming, for example, the tool code could contain something like this
string pathSymbol = Environment.GetEnvironmentVariable("PATH");
string[] pathFolders = pathSymbol.Split(';');
foreach(string folder in pathFolders)
{
if(File.Exists(Path.Combine(folder, "mylibrary.dll"))
{
..... do whatever you need to do with the file
}
}
This example assumes a Windows environment.
This question already has answers here:
How To: Execute command line in C#, get STD OUT results
(17 answers)
Closed 9 years ago.
My C# application requirement is to issue dir command from command line (with J:\MyFolder> as current directory) and receive output from that command in my C# application.
I tried MSDN where issuing command line examples are there like "/c dir" but I would like to retrieve result also.
Can somebody help me with that? Thanks in advance.
http://www.dotnetperls.com/process-start
and http://www.dotnetperls.com/redirectstandardoutput
Y
Ou can use the ProcessStartInfo class to call other exes and .bats etc and redirect the outout and errors back into your c# program. However this is nasty and could be better achieved in direct code probably depending on what logic you are calling in addition to yoir example. Thinhs such as powershell and wmi and basical file and directory handling
Don't do that.
Use DirectoryInfo.
The basic idea is to start a process using "command.com" as the executable and pass the command line as a parameter as well as redirect the stdout back into your program.
I just reviewed these two links: http://www.dotnetperls.com/process-start and
http://www.dotnetperls.com/redirectstandardoutput These are reasonable descriptions of what you need to do.
It really isn't that difficult, mostly just time consuming getting all the details correct!
To test this out prior to programming invoke command.com and ensure you can use it to issue commands. I found my copy of command.com at C:\WINDOWS\system32.
Hope this helps, please ask if more questions.
I'm developing a project in WinForms, and I'm on the process of creating an installer using WiX.
But when the installer is going to copy a .dll that comes from a really long path, Visual Studio says this:
'Really long route'\EnterpriseLibrary....\ is too long, the fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
I found articles that talks about MAX_PATH limitations like said in this StackOverflow question related with the Windows API.
I'm working on a big team, and we just discovered this known error, but we are not allowed to shorten or modify the path.
I tried the solution that the link above says, using the \\?\ characters before, so my WixVariables remain like this:
<?define examplesPath="\\?\$(sys.CURRENTDIR)\..\..\ExamplesFolder" ?>
That results to be something like this:
\\?\C:\reallylongpath\files
But it doesn't seem to work for WiX variables.
So my question is:
Is there any way to avoid this 260 characters limitation? If so, how?
Please, I need an answer on this!
EDIT: While I try #Jans' suggestion, I also found that, if I add the \\?\ string to my WiX variable, the error message changes. Now it says:
The system cannot find the file '\\?\Reallylongpath\..\..\andreallylongfile\'
I'm thinking that maybe the \\?\ is not converting the ..\ that I need to use... Any suggestion here?
EDIT2: I found this line at msdn:
A consequence is that \\?\ turns off file name normalization performed by Windows APIs, including removing trailing spaces, expanding ‘.’ and ‘..’
:___(
This is a terrible hack, but you could create a symlink to the real directory. A symlink is like a regular link, except for that it behaves exactly like a real directory.
Suppose you have a really long directory that causes you trouble:
C:\blahblahblah\thisisreallylong\andnotaccessible\blahblahblah\
You can create a symlink to it, that has any name you like, but is considerably shorter. Think of it as an alias. So if you call this on your console, for example in the C:\temp directory:
C:\temp\>mklink /D reallylong C:\blahblahblah\thisisreallylong\andnotaccessible\blahblahblah\
then afterwards, you can access C:\temp\reallylong as if it were your real directory. Note that you need local admin rights to create symlinks.
I want to make a quick check if in a complete path a Junction point is used. I already have a function to test a folder like IsJunction() but maybe there is an other solution to not call IsJunction() on every subfolder.
So I'm looking for a function like HasJunctionsInPath(string path) without testing each folder of the path.
Is there something which can do this?
Edit:
Or better...
Is it possible to resolve all junctions in a path to get the real location of a file or folder? This would be even better solve my problem and I still can compare the result with the original path to implement a bool HasJunctionsInPath(string path) function.
Look at the solution of Jeff Brown.
He implemented your features in a static class, that seems to just work fine.
http://www.codeproject.com/KB/files/JunctionPointsNet.aspx
As far as I know there's no out of the box implementation in c# to handle junctions / reparse points directly.
So you have to do some interop with kernel32.dll. This is a bit tricky, but you find samples.
You'll need the CreateFile() & DeviceIoControl() calls.
Here is a good sample to start:
http://www.codeproject.com/KB/vista/ReparsePointID.aspx?display=Print
useful msdn links:
http://msdn.microsoft.com/en-us/library/cc232007%28PROT.13%29.aspx
http://msdn.microsoft.com/en-us/library/aa363858%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/aa363216%28v=vs.85%29.aspx
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/7acd564f-0e9f-4295-8609-916af6675f0a