I want to parse ZoneTransfer information of a file using C#.
In PowerShell it can be achieved as the following:
PS D:\Source> Get-Content 7z1900.exe -Stream Zone.Identifier
[ZoneTransfer]
ZoneId=3
ReferrerUrl=https://www.7-zip.org/download.htmlHostUrl=https://www.7-zip.org/a/7z1900.exe
Related
I have a requirement which includes file embedding (text files) into a dynamically generated RTF file.
I’m currently able to generate the RTF without problems but I also need to embed one or multiple txt files into it as well.
I’ve done some research and also looked into rtf spec. Looks like there might be a way to do that with \object tag but as far as I understand \objdata content should be in OLE format. Is this the only way? (I was hoping I could include in byte array format) Or is there any way to convert txt file content into this format so that I can include?
I currently have several microservices written in python and c# (dotnet core) and they are all running on docker containers on linux env. So preferably I need a way that’s suitable with these.
Thank you in advance.
Aspose.Words allows inserting embedded OLE objects into Word documents and it has .NET Standard 2.0 version.
https://apireference.aspose.com/net/words/aspose.words.documentbuilder/insertoleobject/methods/2
I use a pragmatic solution here, which works when text files should be embedded in an RTF document.
Save the desired Document as RTF with dummy text at the position of the text to be replaced.
Open this RTF with a text editor and replace the \par lines with a replacement marker [!!! replace me !!!].
From:
... prefix rtf stuff ...
\par text foo
\par bar text
\par text baz
... suffix rtf stuff ...
To:
... prefix rtf stuff ...
[!!! replace me !!!]
... suffix rtf stuff ...
Save this as template.rtf
In C# read your text file to be embedded into a string prepending each line with \par<space>. Read the template into a second string an replace there the marker with the first string.
Save this as new.rtf.
Here is an example (sorry Perl only, C# should be a 10 liner, too):
#!/usr/bin/perl
use strict;
my $contents = "";
my $first = 1;
while(<>) {
if (!$first) {
$contents .= "\\par ".$_;
} else {
$contents .= $_;
$first = 0;
}
}
my $file = 'template.rtf';
open my $fh, '<', $file or die;
$/ = undef;
my $data = <$fh>;
close $fh;
$data =~ s/\[!!! replace me !!!\]/$contents/g;
print $data;
Call it so:
perl mkrtf.pl <test.lst >test.rtf
I am working with MATLAB. When I create .slx model and use next MATLAB code for insert metadata.
`h = load_system(pathToFile);
set_param(h, 'Metadata', properties)`
MATLAB write my data in .mxarray file in .slx.
How can read metadata of .slx file or from .mxarray with C#.
I am trying to import an XML file into excel using Data -> Other Sources -> From XML Data import. When the file contains a 'µ' symbol, it gives the following error:
Invalid file reference. The path to the file is invalid, or one or
more of the referenced schemas could not be found.
The XML looks like this:
<root>
<File>
<FileName>Data\7.5 µg_mL Sample.pdf</FileName>
</File>
</root>
If i remove the microgram symbol, it works and Excel imports the data.
I am generating the XML file in .net using XNode.toString(), and if I run the XML through a validator, it returns no errors. It doesn't seem to matter if I put the XML declaration at the top of the file and declare it as UTF-8 or 16 either.
Any pointers welcome, i would ideally like to check for any characters that might cause this problem as i am guessing there are more than just the microgram symbol.
I am passing the XML string to a function that swaps out a custom xml file, i don't seem to have the option to change the file format here..
'Uses Ionic.Zip.ZipFile
Using zip As ZipFile = ZipFile.Read(fileDest)
zip.RemoveEntry(xmlPath)
zip.Save()
zip.AddEntry(xmlPath, customXml)
zip.Save()
End Using
Per the docs for the overload of AddEntry you are using:
The content for the entry is encoded using the default text encoding for the machine
You want this to be UTF-8, so you can use the overload that allows you to specify the encoding:
zip.AddEntry(xmlPath, customXml, Encoding.UTF8);
Can anyone suggest how to merge multiple PDF files into a single PostScript file when using GhostScript with C#
Using the command line works and merges the two input PDF files to a single PostScriptFile:
gswin32c.exe -dNOPAUSE -dBATCH -sDEVICE=ps2write -sOutputFile=outfile.ps Infile1.pdf Infile2.pdf
I cannot get it to work in C# though:
params.Add("-q");
params.Add("-dQUIET");
params.Add("-dNOPAUSE");
params.Add("-dBATCH");
params.Add("-P-");
params.Add("-dPARANOIDSAFER");
params.Add("-dNOPROMPT");
params.Add("-sDEVICE=ps2write");
params.Add("-dLanguageLevel=3);
params.Add("-dASCII85DecodePages=false");
params.Add("-sOutputFile=outfile.ps");
params.Add("InFile1.PDF inFile2.PDF");
I did try it with a comma between the output file names as well.
Any suggestions would be greatly appreciated. Thanks, Mike
I would start with something like this:
params.Add("-ignored");
params.Add("-q");
params.Add("-dQUIET");
params.Add("-dNOPAUSE");
params.Add("-dBATCH");
params.Add("-dPARANOIDSAFER");
params.Add("-dNOPROMPT");
params.Add("-sDEVICE=ps2write");
params.Add("-dLanguageLevel=3");
params.Add("-dASCII85DecodePages=false");
params.Add("-sOutputFile=outfile.ps");
params.Add("-f");
params.Add("C:\fullpath\InFile1.PDF");
params.Add("C:\fullpath\inFile2.PDF");
Within c#, i can put data to seperate strings.
For example the current date i put to a string called line1 and some info i put to a string called line2.
What i want to do now, is sent these 2 strings to a web adress that handles these lines, and write them into a simple text file. (or can i write to a text file on a website directly from C# ?)
My knowlage of php is very low, but so far i found this code to be working:
<?php
$File = "name.txt";
$Handle = fopen($File, 'a');
$Data = "line1\n";
fwrite($Handle, $Data);
$Data = "line2\n";
fwrite($Handle, $Data);
print "Data Added";
fclose($Handle);
?>
The C# application is running on a computer, not the website (WPF window).
But now it only has the content of the $Data written to the "name.txt" file.
Does anyone know how i could link the text that is binded to the stings in C3, to the datafields defined in the PHP, so that the text from the strings gets written to the text file on the website? Or would it be possible to write directly to a text file without the php in between ?
So, you have a C# app that you want to use to send 2 bits of data to a PHP based website, and have the website write the data into a file? If that's what you want, you'll need to do something like the following...
On the website, create a receiving PHP file. The bones of it would be something like :
<?php
$File = "name.txt";
$Handle = fopen($File, 'a');
$line1 = $_GET["line1"] . "\n";
fwrite($Handle, $line1);
$line2 = $_GET["line2"];
fwrite($Handle, $line2);
print "Data Added";
fclose($Handle);
echo "Completed writing data to the file";
?>
and to submit that data from the C# app to the website, do something as simple as
WebClient wc = new WebClient();
Console.WriteLine(wc.DownloadString("http://example.com/Receiver.php?line1=this is the first line&line2=and this is the second"));
(
NOTE : No error handling is included in this code, and anyone who knows the URL for the receiver will be able to overwrite your file with whatever they like. Take care when actually implementing this.
ALSO NOTE : It is years since I did much with PHP, so you will probably need to tweak the code.
AND ANOTHER THING : the WebClient.DownloadString approach is as basic as it gets. You may want to look at HttpWebRequests if you need more control
)
You can write to a text file on a website directly from C#.
System.IO.StreamWriter file = new System.IO.StreamWriter(Server.MapPath("/file.txt"););
file.WriteLine("First line.");
file.WriteLine("Secondline.");
file.Close();
It will create a file in the root of your website (the user running the site has to have write permissions in this directory)