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");
Related
I'm using c# fw4.5.
I have a simple code extracting a zip file.
foreach(ZipArchiveEntry entry in z.entries) //z is a zip file open in ZipArchiveMode.Read
{
entry.ExtractToFile(entry.FullName);
}
The zip file have a directory inside it and all files are inside that directory.
When I look at the z.Entries I see its an array which place [0] is only the directory and [1],[2],[3] are files.
But when its try to do:
entry.ExtractToFile(entry.FullName);
On the first entry, I get an error:
"The filename, directory name or volume label syntax is incorrect".
I can't seems to find out whats wrong. Do I need to anything also for it to open the directory? Maybe because the entry is a directory only the "ExtractToFile(entry.FullName)" can't work?
Thanks in advanced.
According to this MSDN article, the ExtractToFile method expects a path to a file (with an extension) and will throw an ArgumentException if a directory is specified.
Since the first entry in the archive is a directory and you are using its name as the argument, that is why you are having this issue.
Look into the related ExtractToDirectory method, which is used like so:
ZipFile.ExtractToDirectory(#"c:\zip\archive.zip", #"c:\extract\");
In addition to Tonkleton's answer I would suggest that you use a third-party compression library since ZipArchive is not supported for framework versions before the .Net 4.5 framework, might I suggest DotNetZip as mentioned in other questions regarding compression in earlier frameworks on StackOverflow.
Replace your paths:
void Main()
{
var zipPath = #"\\ai-vmdc1\RedirectedFolders\jlambert\Downloads\cscie33chap1and2.zip";
var extractPath = #"c:\Temp\extract";
using (ZipArchive z = ZipFile.OpenRead(zipPath))
{
foreach(ZipArchiveEntry entry in z.Entries) //z is a zip file open in ZipArchiveMode.Read
{
entry.ExtractToFile(Path.Combine(extractPath, entry.FullName), true);
}
}
}
I am writing a C# app that needs to create a CSV file.
The problem is after issuing a File.CreateText({file path}), I cannot immediately write to it. If the file already exists, I can write no problem. Here is the code:
if (!File.Exists(file_name))
{
File.CreateText(file_name);
File.WriteAllText(file_name, String.Format("Old Name,Short Name{0}", this.old_name_txt.Text, this.new_name_txt.Text, Environment.NewLine));
}
else
{
File.AppendAllText(file_name, String.Format("{0},{1}{2}", this.old_name_txt.Text, this.new_name_txt.Text, Environment.NewLine));
}
After creating the file, I have also tried "File.AppendAllText..."
The error it is producing says that it cannot write because the file is being used by another process.
File.CreateText will create a text file and open the file. It then returns a StreamWriter which you could use to access that created file. Because you opened the file like this already, you will get an error when trying to open that file again.
When reading the docs for File.CreateText make sure to look at the return value as well, to understand what the function does.
Please read the documentation for File.AppendAllText as well and then change your code accordingly.
Here's the most important part in the docs:
AppendAllText(String, String)
Description
Opens a file, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.
You should be able to solve your problem by yourself now.
I am using SharpZipLib to compress with C#.
but, If any file's name is written in Korean, file name is changed.
I think I need to change encording ?
What should I do?
Please try the following:
ZipConstants.DefaultCodePage = System.Text.Encoding.Default.CodePage;
See http://community.sharpdevelop.net/forums/p/1954/36951.aspx
I am using a text file to have some data there for later purposes. So what I do is to check if file exists, if not I am creating a new file when I need. This gives me error saying that my file is still being used by a different process, but I'm not sure why that is.
This is how I do it. Here, I am checking if file exists which starts when program runs:
private void CreateLastOpenFile()
{
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
if (!File.Exists(file))
{
File.Create(file);
}
}
Now, I am adding some data to it while checking or creating a new file (I am having this in 2 places in my program):
CreateLastOpenFile();
File.WriteAllText(file, data);
What could be wrong here? I have read some examples from the Net, but didn't see anything about closing any files.
Try this. This will close the opened stream during file creation
if (!File.Exists(file))
{
FileStream str = File.Create(file);
str.Close();
}
File.Create is creating a FileStream that locks the file. You should close it. Actually, you don't even need to create a file. File.WriteAlltext will do it for you
You are not closing the stream handle that File.Create() returns.
Suggest you do not use File.Create() in your case. You can just use File.WriteAllText(file, data); - according to MSDN documentation it creates the file if it doesn't exist or overwrites the contents when file exists. After that closes the file stream.
I recommend you to create and fill with data the file in one step, using some class like StreamWriter that allows you to dispose the class, you should not have problem doing it this way, here is an example:
StreamWriter Swr = new StreamWriter(FilePath);
Swr.Write(Data);
Swr.Close();
Swr.Dispose();
//Doing the close and Dispose you get sure the file is not locked anymore
You can also use File.WriteAllText(string Path, string Data), this method does not lock the file.
If you are using below method to write the data into text file, you dont need to check if file exists and if not create it. "WriteAllText" takes cares of all these things by itself. It will create the file if not exists, write the data and close it, or overwrite the file if already exists.
File.WriteAllText(file, data);
if you are using writeAllText() or readAllText() method than close() method is not used as they closed file after reading or writing(above methods)
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?