Txt file as config file [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I found application that use .txt files as config files.
The Files looks like
[[plugin.save]]=[[Save]]
how do I use it in C#?
how can I read The file to my application to use Values in [] as config?

You have to read it as a regular file. Reading it use Dictionary to store values.
Example code:
Dictionary<string, string> configuration = new Dictionary<string, string>();
Regex r = new Regex(#"\[\[(\w+)\]\]=\[\[(\w+)\]\]");
string[] configArray = {"[[param1]]=[[Value1]]", "[[param2]]=[[Value2]]"};// File.ReadAllLines("some.txt");
foreach (string config in configArray)
{
Match m = r.Match(config);
configuration.Add(m.Groups[1].Value, m.Groups[2].Value);
}
Please note to check for possible null values.
Note also that regex expression should be different if config values can contain for example spaces.

Just use:
var config = File.ReadAllLines(FileLocation)
And then parse it with the
String.Split()
Might use regex as well.

Related

How to export CSV to Db with headers in C#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How to export csv to db in C#. It works fine when you have a plain text simple text. But in my case I need to parse headers as single row.
There are many options!
use some driver custom functions (depends on your database)
use a [schema.ini](
https://learn.microsoft.com/en-us/sql/odbc/microsoft/schema-ini-file-text-file-driver)
do it manually ...
You can do it manually with while iteration. Before using loop, you should read csv file from path with StreamReader.
For example:
using(var csvReader = new StreamReader(System.IO.File.OpenRead("your file path")))
{
while (!csvReader.EndOfStream)
{
var rows = csvReader.ReadLine();
var columns = rows.Split(';');
if(values.Length > 1)
{
// Your logic getting values to save db
}
}
}

Regular expression to search for files and folders [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Help write a regular expression to search for files and folders,
searches for a given mask. In the mask, you can use "*"
(any characters in any number), and the "?" (one symbol).
Here shows you how to use regex in C#.
You could always just loop the directory that you're looking in and check the file names instead of making a regex. (You'll need to use System.IO)
Perhaps something like this?
string [] fileEntries = Directory.GetFiles(targetDirectory);
Regex regex = new Regex("target file name");
Match match = regex.Match(string.Join(" ", fileEntries););
if (match.Success)
{
Console.WriteLine(match.Value);
}

C# How to modify text file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need to read accounts.txt and add/change number after password
Here is accounts.txt
user|password
user1|password1
After starting
user|password|1
user1|password1|1
After closing
user|password|0
user1|password1|0
Sorry for my english
Add this reference first System.IO Then:
For reading:
string[] accounts= File.ReadAllLines("accounts.txt");
//each item of array will be your account
For modifying :
accounts[0] += "|1";//this adds "|1" near password
accounts[0].Replace("|1","|0"); this will change the "|0" text to "|1"
And For writing:
File.WriteAllLines("accounts.txt",accounts);
Look at System.IO.File class, as well as general System.IO.Stream and System.IO.StreamReader classes.
There are a bunch of examples on the internet on how to read and write text files in .NET: http://msdn.microsoft.com/en-us/library/db5x7c0d(v=vs.110).aspx
Something like that :
string[] lines = File.ReadAllLines(#"C:\filepath.txt");
List<string> newlines = new List<string>();
foreach(string line in lines)
{
string[] temp = line.Split('|');
newlines.Add(temp[0] + "|" + temp[1] + "|" + "1");
}
File.WriteAllLines(#"C:\filepath.txt", newlines.ToArray())

Dictionary values come back with slashes '\' removed? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I saved few directory locations to a Dictionary<string, string>,
e.g.
C:\\WINDOWS\\system32\\abc\\123
But the value that gets stored in the dictionary is C:\WINDOWS\system32\abc\123
So when I later compare a value against one in the dictionary it does a comparison like this:
C:\WINDOWS\system32\abc\123
to this
C:\\WINDOWS\\system32\\abc\\123
How can I retain backslashes when storing values in a Dictionary?
Try this:
Dict.Add(key, #"C:\\WINDOWS\\system32\\abc\\123");
\ is an escape character. Adding # makes your string a string literal instead.
EDIT I've reproduced your issue and this fix will solve it.
Use the # symbol in front of the strings when saving. That should solve it.

Get Resources with string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a lot of txt files in Resources folder. One of them is corner.txt. I can access this file via this code snippet:
Properties.Resources.corner
I keep file names in string variables. For example:
string fileName = "corner.txt";
I want to access this file via:
Properties.Resources.fileName
Is this possible? How can I access?
I solved the problem this code snippet:
string st = Properties.Resources.ResourceManager.GetString(tableName);
So, I don't use the filename, I use the txt file's string. This is useful for me.
Thanks a lot.
You can use Reflection like that:
var type = typeof(Properties.Resources);
var property = type.GetProperty(fileName, BindingFlags.Static| BindingFlags.NonPublic|BindingFlags.Public);
var value = property.GetValue(null, null);
or use the ResourceManager like that:
value = Properties.Resources.ResourceManager.GetObject(fileName, Properties.Resources.Culture);

Categories