I'm outputting two files from my program, the paths to which are supplied in user config.
I need to reference one file from the other so was happy to find the Path.GetRelativePath function however, this:
Path.GetRelativePath("/Users/ben/file1.txt", "/Users/ben/file2.txt")
outputs ../file2.txt
I can't see how that's correct; I would expect simply file2.txt or better still ./file2.txt
I'm using .net 6.0 on MacOS
There are lots of examples here of how to do this using Uri's, but can someone explain why this isn't doing what I'd expect...
Thanks
Thanks to #Alexey Larionov (see comments); I simply hadn't read the documentation properly; it states "[The first property] is always considered to be a directory".
So easy fix is
Path.GetRelativePath(
Path.GetDirectoryName("/Users/ben/file1.txt"),
"/Users/ben/file2.txt")
//returns file2.txt
Related
I have following, simple, problem because I don't have the luxury to actually debug properly right now. This question might even seem dumb to most but I am still unexperienced when it comes to coding.
I am supposed to write a simple console application that connects to a few different clients (and one server) and replaces some files on the C Drive with Adminrights(If I understood it right that's what the C$ is for?). But because something had gone wrong in the past there are actually a few occurences where the filepath differs from the standard, so I have to check with Dictionary.Exists(path) first, to not make it any longer, what is the correct Syntax to properly connect to said folder?
I was thinking about:
Directory.Exists(#"192.168.xxx.xxx\C$\Program Files\...")
I also tried to test it via localhost, but that didn't seem to work because either I am doing it wrong or it is just not intended to work with it?
Well, figured it out now thanks to a tip from Alex K.. Didn't know that this kind of format was called UNC and did some research about it (a bunch of different sites), could easily test it on my own computer like that:
bool test = Directory.Exists(#"\\192.168.10.102\C$\Program Files")
Also works with the domainname "localhost" instead of "192.168.10.102". Both resulted in true.
I have an xml file that I want to include along with my program as a template. I would prefer that it be bundled with the .exe when the project is completed.
Is this possible?
If so, how should I reference it in the code? I would assume that referencing "myXML.xml" won't work because, if it is included, the file no longer exists as a standalone object.
An alternative idea is to copy/paste the contents into string, but that seems like a bad idea in so many ways. (It's 900 lines.)
Ideas?
Thank you.
The real motivation behind this is I really prefer standalone executables rather than making the user go through the installation process. Additionally there's the extra benefit that they're less likely to f- it up.
This is quite simple to do, you can store files in any .NET assembly as "Embedded Resources", which can be then accessed at runtime.
See the Microsoft article here for a detailed rundown on how to do this.
Another way is resource files, http://msdn.microsoft.com/en-us/library/7k989cfy%28v=vs.80%29.aspx
For one reason or another I have a PATH variable which has multiple entries where my exe can be located.
I am struggling to find the order that Windows uses when resolving DLLS.
Does it scan the Path variable and the first (in the list) match is used? In my testing it looks like the final one is used.
Does anyone know what the behaviour of this is and whether it is documented anywhere?
I believe that the last one that is read wins.
For .NET, I don't think that the PATH is used. Here is an article that explains CLR probing in more detail.
EDIT:
Here's a better article from MSDN.
Also this article has a nice flowchart that explains some of the assembly loading logic.
I'm trying to check the validity of a particular 'stand-alone' code file from within my C#.Net app. Is there any way that I can check the file and get a visual studio's style errors list out?
I'm only interested in running a basic check to ensure that the basics would validate. I.e. all variables are declared and method names are valid.
Is this at all possible?
If it's not the same as the referred to question (see my comment), you mention "VS style errors", consider using the CSharpCodeProvider. An example of its usage is in its documentation at MSDN.
Call the C# command line compiler (csc.exe)?
You can use static code analysis. There are a few libraries that enable this. The one that cones to mind immediately is StyleCop. It is written in .NET so you should be able to reference and use it through code if that is what you are looking for.
I want to create my own incremental backup solution using C#. How can I obtain the difference between two files (version 1 and version 2 of ABC.TXT) and then update ABC.TXT version 1 with the difference? Would appreciate some hints! Thank you!
here are some articles to some diff algorithms explained in C#
Codeproject 1
Codeproject 2
Its not easy to get this algorithm right. I would suggest executing kdiff3 or some other good diffing tool in a background process rather than writing it yourself.
Here's something I just Googled, might be helpful as a starting point:
An O(ND) Difference Algorithm for C#
Do you really need incremental backup? Is there any reason why you can't just replace version 1 with version 2?
And as Jon pointed, probably you'd better use an already existing and tested backup solution.
I'm not sure as to how exactly you would replace the 'difference' text as that could get quite complex. But for the initial checking, you could compare the file sizes.
The link below might help you out:
http://dotnetperls.com/file-size