Deleting particular text in a file using c#? - c#

My text file named test.txt contains
Fixing the type manager error during checkin of the elements and supporting issues during Checkin/Checkout & merge related problems.
Now i want to remove the text "supporting issues" using c#.
Please anybody let me know.
Thanks in advance,
Naveenkumar.T

if the file is fairly small you can do this:
using System.IO; // at the top of the file
string file = "path.txt"; // change to the path
// read the whole file into a string, make a replacement, then write all string to a file
File.WriteAllText(file, File.ReadAllText(file).Replace("supporting issues ",""));
Hope that helps.

Related

C# Cant find file (read from serial port)

I´m trying to open a file named the same as a bar code but I get the error that the file cannot be found?
I use this line to read the file:
string[] DxfFile = System.IO.File.ReadAllLines(textBoxBarCodeScanner.Text);
It works fine and open the file correctly if I assign the text box with:
textBoxBarCodeScanner.Text = (#"PLANKA.DXF");
I use these lines to read from the serial port:
RecievedData = RecievedData.Replace("\r\n", "").Replace("\r", "").Replace("\n", "");
textBoxBarCodeScanner.Text = (#RecievedData);
I had to remove /r first but it did not help.
I´m kind a beginner so I´m not so good at finding the right debug information for you so here is what I got and please tell me where I found more useful information.
If I break at the exection and look at "locals" I have a row that says text, and there I got "PLANKA.DFX" which seem to be correct.
Debug error message is as follow:
Additional information: Could not find file E:\Win7\Google
Drive\Visual Studio 2013 Projects\Husmaskin GUI\Husmaskin
GUI\bin\Debug\PLANKA.DFX.
This works (#"PLANKA.DXF"):
This does not (#RecievedData)??
In your screenshots, you have two different values for the file extension: DXF and DFX.
There may be extra whitespace around the incoming text, so I would suggest also adding .Trim() to your code.
I would also suggest checking for the file in your code and creating an error message that is more useful to you:
var filename = txtFoo.Text;
if (!File.Exists(filename))
throw new Exception($"Could not find file '{filename}'");

Open a file without dialog

with openFileDialog you would select a file and after pressing "OPEN" it would paste the filepath of the selected file (c:\blob\template) into an textbox.
I would like to do automatically select the file c:\blob\template en then put the filepath in the textbox. basically the exact same thing as openfiledialog without a dialog. i have been trying to do this for some time now. can somebody help me out with this? i have no clue how to realise this.
i`m only able to get the filepath and paste the string in the textbox but this but only fills the box with a string. i need to load the file/template in there.
private void txt()
{
string fileName = "template";
string fullPath;
fullPath = Path.GetFullPath(fileName);
lblFirstTemplate.Text = fullPath;
}
Thank you in advance!
The code you now have will only get the file path. What you need to add is code that will actually open the file and read its content.
Let's say your file contains some text. You can use the following line to read the complete file as text:
System.IO.File.ReadAllText(fullPath);
If your file contains some other data like binary data, you can use:
System.IO.File.ReadAllBytes(fullPath);
And instead of reading all data at once, you can read it one line or a couple of bytes at a time. A good place in the documentation to start is: Common I/O Tasks

How to give path to an text file in Solution using XDocument?

I want to load a xml document Swedish.xml which exists in my solution. How can i give path for that file in Xamarin.android
I am using following code:
var text = File.ReadAllText("Languages/Swedish.txt");
Console.WriteLine("text: "+text);
But i am getting Exception message:
Could not find a part of the path "//Languages/swedish.txt".
I even tried following lines:
var text = File.ReadAllText("./Languages/Swedish.txt");
var text = File.ReadAllText("./MyProject/Languages/Swedish.txt");
var text = File.ReadAllText("MyProject/Languages/Swedish.txt");
But none of them worked. Same exception message is appearing. Build Action is also set as Content. Whats wrong with the path? Thanks in advance.
Just try with this
string startupPath = Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName, "Languages", "Swedish.txt");
var text = File.ReadAllText(startupPath);
Try...
Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments)+"/Languages/Swedish.txt"
If you mark a file as Content Type, it will be included in the app bundle with the path that you are using within your project file. You can inspect the IPA file (it's just a renamed zip) that is created to verify that this is happening.
var text = File.ReadAllText("Languages/Swedish.txt");
should work. The file path is relative to the root of your application. You need to be sure that you are using the exact same casing in your code that the actual file uses. In the simulator the casing will not matter, but on the device the file system is case sensitive, and mismatched casing will break the app.
I've looked into this before and never found any solution to access files in this way. All roads seem to indicate building them as "content" is a dead end. You can however place them in your "Assets" folder and use them this way. To do so switch the "Content" to "AndroidAsset".
After you have done this you can now access the file within your app by calling it via
var filename = "Sweedish.txt";
string data;
using
(var sr = new StreamReader(Context.Assets.Open(code)))
data = sr.ReadToEnd();
....

Open CHM (help file) in C#

I'm trying to open help file (chm extension) in C#.
File.Open(#"//help.chm",FileMode.Open, FileAccess.Read, FileShare.Read);
and
FileStream fileStream = new FileStream(#"c:\help.chm", FileMode.Open);
doesn't work :(
You can use -
System.Windows.Forms.Help.ShowHelp(Control, String)
So assuming you are in a Form/Control
Help.ShowHelp(this, "file://c:\\helpfiles\\help.chm");
ShowHelp method also provides overloads to go to specific topic and help page located inside the compiled HTML help file.
Read System.Windows.Forms.Help.ShowHelp on MSDN
Decompiling a CHM file
Is as easy as executing below command in the command prompt.
hh.exe -decompile <target-folder-for-decompiled-content> <source-chm-file>
For Example:
hh.exe -decompile C:\foo\helpchmextracted help.chm
After executing the above command you should find the decompiled content in the C:\foo\helpchmextracted folder.
string helpFileName = #"c:\help.chm";
if (System.IO.File.Exists(helpFileName))
{
Help.ShowHelp(this, helpFileName );
}
if this is not work try
if (System.IO.File.Exists(helpFileName))
{
System.Diagnostics.Process.Start(helpFileName);
}
Adding my comments to an answer as per request:
It seems that the file name in the first statement is not correct however the second one
should work unless the file is locked, not exists or you don't have permissions to access the file. If you want to ShellExecute the file then you should use System.Diagnostics.Process class, but if you want to extract the contents of the CHM, since is compiled and formatted, it can't be read like plain text files.
Take a look at these links:
Decompiling CHM (help) files with C#
CHM Help File Extractor
Well the second line should be ok, if the file is not existent it should throw an exception. Need to be more specific about what you mean by " it doesn't work"
Help.ShowHelp(this, AppDomain.CurrentDomain.BaseDirectory+"\\test.chm", HelpNavigator.Topic, "Welcome.htm");
Welcome is id of the welcome age in chm file
System.Diagnostics.Process.Start(#"c:\help.chm");
Simple do this
Help.ShowHelp(ParentForm, "chmFile.chm", "link.htm");

Copy one Xml Document contents into another one in C#

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?

Categories