I have a list of files and need to retrieve the filename(s) (Without extension) from file full path.
I generated a list print in command prompt (with path inside intentionally); and I need to replace the path with something else.
i.e:
[This is the *.txt file with generated content example]
C:\folder\files\img1.png
C:\folder\files\img2.png
C:\folder\files\img3.png
C:\folder\files\img4.png
...etc...
What im trying to achieve:
Img1.png. or Img1
I'm using the code bellow, but I don’t know how to use it correctly yet.
[Original Code]
File.WriteAllText("Path", Regex.Replace(File.ReadAllText("Path"), "[Pattern]", "Replacement"));
[The same code; but modified]
File.WriteAllText(#"C:\Folder\Files\print.txt", Regex.Replace(File.ReadAllText(#"C:\Folder\Files\print.txt"), "[C:\folder\files\]", "Copy "));
This is how you can use it. You do not need to use RegEx in this case, because the string which you want to replace is well defined:
File.WriteAllText(#"C:\Folder\Files\print.txt", File.ReadAllText(#"C:\Folder\Files\print.txt").Replace(#"C:\folder\files\", "Copy "));
Couldn't you just use GetFileName and provide the string-value(s)? You can append the output to a string-builder and just rewrite the file.
Related
I'm creating a process in my application that makes it possible to run commands like you do it in the command prompt.
When I want to export a registry key, I use REG EXPORT path file to access the registry key and export the information into the file. If I use a path which contains no whitespaces at all, everything works fine. But as soon as I use a path with whitespaces, e.g. HKEY_CURRENT_USER\Software\NVIDIA Corporation, it does not work.
I'm appending the path as a String with a StringBuilder to the command, the String itself looks like this: String path = "HKEY_CURRENT_USER\\Software\\NVIDIA Corporation".
Do I have to change the String contained in path? Or is there a special static method I can use to format the String?
You probably need to surround the path with quotes.
This might help:
String path = "\"HKEY_CURRENT_USER\\Software\\NVIDIA Corporation\"";
I am wondering how to remove the version number from a file path in a Windows Form Application.
Currently I wish to save some users application data to a .xml file located in the roaming user profile settings.
To do this I use:
get
{
return Application.UserAppDataPath + "\\FileName.xml";
}
However this returns the following string:
C:\Users\user\AppData\Roaming\folder\subfolder\1.0.0.0\FileName.xml
and I was wondering if there is a non-hack way to remove the version number from the file path so the file path looks like this:
C:\Users\user\AppData\Roaming\folder\subfolder\FileName.xml
Besides parsing the string looking for the last "\", I do not know what to do.
Thanks
Use Directory.GetParent method for this purpose.
get
{
var dir = Directory.GetParent(Application.UserAppDataPath);
return Path.Combine(dir.FullName, "FileName.xml");
}
Also note that I've used Path.Combine instead of concatenating paths, this method helps you to avoid so many problems. Never concatenate strings to create path.
I want to copy a file to a directory. I thought it would be a simple enough process.
This is the code im using:
string strSrcPath = "C:\Users\Documents\Development\source\11.0.25.10\",
strDstPath = "C:\Users\Documents\Development\testing\11.0.25.10\",
strFile = "BuildLog.txt"
File.Copy(Path.Combine(sourcePath, sourceFile), strDstPath);
The problem here is that when i'm doing the File.Copy it wants to copy one file to another, but I dont want to do that since the file does not exist in the destination path. Therefore I get thrown an error which states something along the lines of 'Cannot copy, strDstPath is a destination not a file"
Was there something I could use instead of File.Copy to copy a file that doesnt exist in the destinaion from the source to destination?
The problem is that the parameters are the source filename and the destination filename. You are passing a destination directory and the program is confused because you can't make the file into a directory.
Use instead:
File.Copy(Path.Combine(strSrcPath , strFile ), Path.Combine(strDstPath, strFile);
You seem to be passing some wrong parameter to the Path.Combine (the second one). It should be strFile instead of sourceFile which is quite unclear where is it coming from.
And you also need to provide a filename for the destination folder:
File.Copy(Path.Combine(sourcePath, strFile), Path.Combine(strDstPath, strFile));
You also need to escape the \ characters in your string because your code will probably not compile. This could be done by either using \\ or by using the # character at the beginning of your string.
string strSrcPath = #"C:\Users\Documents\Development\source\11.0.25.10\",
strDstPath = #"C:\Users\Documents\Development\testing\11.0.25.10\",
strFile = "BuildLog.txt"
File.Copy(Path.Combine(sourcePath, strFile), Path.Combine(strDstPath, strFile));
Also make sure that the destination folder you specified exists. If it doesn't exist you need to create it first (using the Directory.CreateDirectory method).
You have to specify a filename for your destination
so
File.Copy("XMLFile1.xml", #"c:\temp");
will fail where
File.Copy("XMLFile1.xml", #"c:\temp\XMLFile1.xml");
will not
I use coded UI to run test and get the test result file named like qian_machinename 2011-12-21 14_26_10. I want to read the file and send a test report. My question is how can I get the file time every time I run the tests?
TestContext has 3 properties which you can use
1. TestDir
2. TestDeploymentDir
3. TestResultsDirectory.
You can use these properties to navigate to the folder you are interested in and then get the result file for your processing.
QianLi,
Perhaps you can get the proper output file by using a known pre-fix on the test output filename.
In Visual Studio navigate Test->Edit Test Settings->(Select your active .testsettings)->General
In the prompt that displays you will see an area for naming scheme. By default this is set to name your output file "USER#MACHINE DATE TIME". You can create a user defined scheme and use that to locate the file i.e. store "MyTestOuput" as a pre-fix and then later in code you can examine the file creation date/time if necessary to verify you have the correct output.
Use something Like :
FileName=
testContext.ResultsDirectory + "\" + testContext.TestName.ToString()+".extension"
Testname should be the name of the testMethod Like "T1".
Extension could be any valid file type e.g. .xml etc.
[TestCleanup()]
public void MyTestCleanup()
{
string nomfichiersource = "UITestActionLog.html";
string nomTest = TestContext.TestName.ToString();
string sourcefile = System.IO.Path.Combine(TestContext.TestResultsDirectory, nomfichiersource);
string destfile = System.IO.Path.Combine(#"X:\Temp", nomTest + ".html");
System.IO.File.Copy(sourcefile, destfile);
}
I am trying to copy contents of one xml file into another xml file. I found many examples where copying nodes is done but could not find how to just copy all the content.
Is this possible at all? If so can you please provide some direction.
Thanks
Edit:
I want to create this new xml file in the location dynamically supplied by the application's text box.
Thanks again.
If you want to replace one XML file with another, why not use File.Copy?
As a file:
string sourcefile = "somefile.xml";
string destinationfile = "anotherFile.xml";
System.IO.File.Copy(sourcefile, destinationfile);
Is File.Copy() what you're looking for?