This is the simple C# code I wrote :
string file = #"D:\test(2021/02/10).docx";
var fileName = Path.GetFileNameWithoutExtension(file);
Console.WriteLine(fileName);
I thought I would get the string "test(2021/02/10)" , but I got this result "10)".
How can I solve such a problem?
I just wonder why would you want such behavior. On windows slashes are treated as separator between directory and subdirectory (or file).
So, basically you are not able to create such file name.
And since slashes are treated as described, it is very natural that method implementation just checks what's after last slash and extracts just filename.
If you are interested on how the method is implemented take a look at source code
I am firing up Visual C# (2010 Express) for the first time, and I've created a new project for a WindowsFormsApplication. No code has been written yet, but I created a button and placed it on Form1. Then, I double-clicked the button and am taken to the part of the code where you write what happens when the button is push/clicked.
The first thing I would like to do is read data from a LARGE tab-delimited text file (30MB). The text file contains 7 structured columns of data like names, age, favorite color, animal, etc. Nothing tricky or fancy in the text formatting. I'm using the code below:
File.ReadLines(sourceFilePath)
.Select(line => line.Split('\t'))
.ToArray();
But my more basic question is how do I establish and define File and sourceFilePath? With the code above I get "The name 'File' does not exist in the current context.
You need to add the following line to the top of your C# file:
using System.IO;
This will allow the use of the File class, which is in the System.IO namespace.
As for defining sourceFilePath, that's just a variable, which you can declare and set to whatever file path you need, e.g.
string sourceFilePath = #"c:\data\file.csv";
Note the use of # before the string literal; this prevents the backslashes from being treated as the start of escape sequences. You can instead just escape the bakslashes, e.g.
string sourceFilePath = "c:\\data\\file.csv";
If you want to split by tab key then you can try using ReadAllText method, and then a Split method, where you define delimiter (by tab):
string[] delimitedByTab = File.ReadAllText(#"file").Split('\t').ToArray();
And use System.IO; namespace for File class
The File class resides the System.IO namespace. You leverage it in your application with the following:
using System.IO;
As for defining the contents of sourceFilePath, you are going to need either to hard-code the value to a fixed location, which is not always advisable, or devise a mechanism for the user to specify that path, possibly through one of the various CommonDialogs that are available. Some research on the CommonDialogs should help push you a bit further in developing your project.
I currently have two strings assigned - domain,subdomain
How could I delete any matched occurrences of these strings in a text file?
string domain = "127.0.0.1 test.com"
string subdomain = "127.0.0.1 sub.test.com"
I don't think using a regex would be ideal in this situation.
How can this be done?
You need to:
Open the existing file for input
Open a new file for output
Repeatedly:
Read a line of text from the input
See if it matches your pattern (it's unclear at the moment what pattern you're looking for)
If it doesn't, write the line to the output (or if you're only trying to remove bits of lines, work out which bit you want to write out)
Close both the input and output (a using statement will do this automatically)
Optionally delete the original file and rename the new one if you want to effectively replace the original.
var result = Regex.Replace(File.ReadAllText("file.txt"),
#"127\.0\.0\.1 test\.com|127\.0\.0\.1 sub\.test\.com", string.Empty);
Then write to file obtained result.
Working on a program that takes a CSV file and splits on each ",". The issue I have is there are thousand separators in some of the numbers. In the CSV file, the numbers render correctly. When viewed as a text document, they are shown like below:
Dog,Cat,100,100,Fish
In a CSV file, there are four cells, with the values "Dog", "Cat", "100,000", "Fish". When I split on the "," to an array of strings, it contains 5 elements, when what I want is 4. Anyone know a way to work around this?
Thanks
There are two common mistakes made when reading csv code: using a split() function and using regular expressions. Both approaches are wrong, in that they are prone to corner cases such as yours and slower than they could be.
Instead, use a dedicated parser such as Microsoft.VisualBasic.TextFieldParser, CodeProject's FastCSV or Linq2csv, or my own implemention here on Stack Overflow.
Typically, CSV files would wrap these elements in quotes, causing your line to be displayed as:
Dog,Cat,"100,100",Fish
This would parse correctly (if using a reasonable method, ie: the TextFieldParser class or a 3rd party library), and avoid this issue.
I would consider your file as an error case - and would try to correct the issue on the generation side.
That being said, if that is not possible, you will need to have more information about the data structure in the file to correct this. For example, in this case, you know you should have 4 elements - if you find five, you may need to merge back together the 3rd and 4th, since those two represent the only number within the line.
This is not possible in a general case, however - for example, take the following:
100,100,100
If that is 2 numbers, should it be 100100, 100, or should it be 100, 100100? There is no way to determine this without more information.
you might want to have a look at the free opensource project FileHelpers. If you MUST use your own code, here is a primer on the CSV "standard" format
well you could always split on ("\",\"") and then trim the first and last element.
But I would look into regular expressions that match elements with in "".
Don't just split on the , split on ", ".
Better still, use a CSV library from google or codeplex etc
Reading a CSV file in .NET?
You may be able to use Regex.Replace to get rid of specifically the third comma as per below before parsing?
Replaces up to a specified number of occurrences of a pattern specified in the Regex constructor with a replacement string, starting at a specified character position in the input string. A MatchEvaluator delegate is called at each match to evaluate the replacement.
[C#] public string Replace(string, MatchEvaluator, int, int);
I ran into a similar issue with fields with line feeds in. Im not convinced this is elegant, but... For mine I basically chopped mine into lines, then if the line didnt start with a text delimeter, I appended it to the line above.
You could try something like this : Step through each field, if the field has an end text delimeter, move to the next, if not, grab the next field, appaend it, rince and repeat till you do have an end delimeter (allows for 1,000,000,000 etc) ..
(Im caffeine deprived, and hungry, I did write some code but it was so ugly, I didnt even post it)
Do you know that it will always contain exactly four columns? If so, this quick-and-dirty LINQ code would work:
string[] elements = line.Split(',');
string element1 = elements.ElementAt(0);
string element2 = elements.ElementAt(1);
// Exclude the first two elements and the last element.
var element3parts = elements.Skip(2).Take(elements.Count() - 3);
int element3 = Convert.ToInt32(string.Join("",element3parts));
string element4 = elements.Last();
Not elegant, but it works.
Is it possible to get the extension of a file but when you specify the entire path other than the extension? For example:
C:\Users\Administrator\Pictures\BlueHillsTest
Thanks
Directory.GetFiles will allow you to specify a wildcard for files to search:
System.IO.Directory.GetFiles(#"C:\temp\py\", "test.*")
for me, returns an array of 3 matching items. I expect an array, since the directory contains test.cover, test.py, and test.pyc.
If I use the First extension method:
System.IO.Directory.GetFiles(#"C:\temp\py\", "test.*").First()
then it only returns the first result (test.cover).
However, using the Single extension method:
System.IO.Directory.GetFiles(#"C:\temp\py\", "test.*").Single()
raises an InvalidOperationException because the "Sequence contains more than one element" (which might be what you want, depending on your circumstances).
But if I try
System.IO.Directory.GetFiles(#"C:\temp\py\", "step.*").Single()
then I get just step.py (no exception raised) because that's the only file matching step.* in that directory.
No it is not possible as you might have both BlueHillsTest.xxx and BlueHillsTest.yyy in this location. Which one do you expect to return in this case?