I want to view presentation in PowerPoint viewer, ppt file is in a resources. so the problem is that how can i access it and view in PowerPoint viewer.
Here is sample code
Process.Start(#"C:\Program Files\Microsoft Office\Office12\PPTVIEW.exe",**#"e:\presentation.ppt")**;
How can i replace this path by ppt containing in resources?
Actually, what you ask for is a common pattern and there are some related questions and answers here on SO.
Basically what you do in general is the following:
locate the resource in question and open a resource stream to it.
Save the stream to a (temporary) file if your target API cannot deal with streams or byte arrays directly.
Perform whatever operation on the file or directly on the stream/byte array (as I said, if supported).
Eventually remove the temporary file, if any, from step 1.
So, you first extract the PPT file (actually it doesn't really matter that it is a PPT file, could by any file or byte blob for that matter).
string tempFile = Path.GetTempFileName();
using (Stream input = assembly.GetManifestResourceStream("MyPresentation.PPT"))
using (Stream output = File.Create(tempFile))
{
input.CopyTo(output); // Stream.CopyTo() is new in .NET 4.0, used for simplicity and illustration purposes.
}
Then you open it using Process.Start(). You don't need to specify the path to the Powerpoint executable, as PPT should be a registered file extension with either PowerPoint or the PowerPoint Viewer. If you have both installed, you may still want to provide the path to the relevant executable to prevent launching the wrong application. Make sure that you don't hardcode the path though, but try to retrieve it from the registry (or similar, I haven't checked because that gets too specific now).
using (var process = Process.Start(tempFile))
{
process.WaitForExit();
// remove temporary file after use
File.Delete(tempFile);
}
Note: I left out quite some error handling that you might want to add in a real application.
Related
I am trying to split a 'n' paged pdf file to 'n' number of pdf files containing 1 page each in ".net". For normal pdf files, PDFSharp is working fine but for corrupt file its showing errors listed down.
When I use Adobe Reader and 'Save As' the file, the new file is uncorrupted one. But I do not want to do it manually. I tried to open the pdf in Adobe reader using 'Process' but I can't save from there without manually saving it. If I use other DLLs the job gets done but it adds watermark.
Errors while opening the PDF Doc:
"Invalid entry in XRef table, ID=9, Generation=0, Position=0, ID of referenced object=1, Generation of referenced object=0"
{"Unexpected character '0xffff' in PDF stream. The file may be corrupted. If you think this is a bug in PDFsharp, please send us your PDF file."}
Object already in use exception.
For handling corrupt files through process I tried this:
Process p = new Process();
p.StartInfo.FileName = file;
p.Start();
p.Close();
corrupt = true;
inputDocument = PdfReader.Open(file, PdfDocumentOpenMode.Import);
Dealing with a corrupt file gracefully is no simple task. You need a deep understanding of the file format. Extensive examples of what could go wrong from other broken implementations. And a strategy to resume parsing for each type of error.
If there isn't a nice pdf library that already has these features, you aren't going to find one here.
Can i Convert an spss(.sav) file to a .csv file by using C#. Here i want to browse 1 .sav file and i need to generate .csv file of the same.Can anyone provide any link or code for the conversion.
i have some 100 spss files so i need to create a console app which will take each file from the parent folder and generate the corresponding csv file for each sav file
There are several possibilities:
1) Use a library
There seems to be a library to read SPSS files.
You can install the NuGet package SpssLib and create a SpssReader object from a FileStream
using(var fileStream = File.OpenRead("spss-file.sav"))
{
var spssReader = new SpssReader(fileStream);
// read variable names from spssReader.Variables and store to CSV
// read records from spssReader.Records and store to CSV
}
2) Hand-code the solution
If you can't use the library for whatever reason, you may hand-code a solution.
2.1) Have a look at PSPP
If (and only if) you are planning (or at least fine with) releasing your code under the GPL, you can have a look at the PSPP source code. Anyway, if you can't GPL your code, Don't. Do. It. Do a clean-room implementation instead, since otherwise you'll always be on slippery grounds.
2.2) Have a look at the spec
There is a documentation of the SAV file format online. It may take some time, but you may eventually figure out how to convert this to CSV.
3) Use PSPP
If you have no problem shipping a GPLed software (or are able to download the files on demand somehow), you can make use of PSPPs console application. PSPP is a GNU replacement for SPSS, which aims at (but does not, yet) providing the funcitonality that SPSS provides. Obviously it comes with a handy little CLI tool pspp-convert (see the documentation). You can invoke it via the command line with
pspp-convert input.sav output.csv
With the help of the Process class you're able to start another process (i.e. program in this case). If pspp-convert is located in the current directory or in any directory that's in the PATH variable, converting a file to CSV is a easy as
public ConvertSpssFile(string inputFile)
{
var outputFile = Path.ChangeExtension(inputFile, "csv");
Process.Start("pspp-convert", $"{inputFile} {outputFile}");
}
I have a Windows Forms application for which I have written an extensive manual in Word with helpful pictures. Is there a way to include the .docx in the .exe so that users can click a button in the application to open to manual in Word?
Instead of "attach" I think you mean "embed".
And yes, you can, by storing it as an Embedded Resource.
There are three ways to embed resources in .NET Win32 programs:
As a Win32 PE Resource. This is not easily used from within .NET code as there's no built-in API for them (so you'll need P/Invoke (DllImport)), but means that other programs can acces the resources.
As an Embedded Resource Stream stored directly, this is accessed using the Assembly.GetManifestResourceStream() API in the .NET Framework. This is the fastest way (as it's exposed as a Stream instead of a byte array, so it isn't loaded into memory unnecessarily).
As a Byte[] inside a .NET .resx/.resources resource. This is suboptimal because the file is wrapped in another abstraction and exposed as a Byte[] instead of a Stream, but you can more easily manage the files in Visual Studio.
I recommend the GetManifestResourceStream method, so do this:
In your project, include the Word document in the Project (Solution Explorer > Show All Files > (your doc, right-click) > Include File
Select the file then open Properties and under "Build Action" choose "Embedded Resource"
Note that the project-root relative folder-path will be translated into a dot-separated name prefix for your resource, so if the file is My Project\Documents\Readme.doc then the resource name will be Documents.Readme.doc. You can override this, but you will need to edit your .csproj file directly (using the <LogicalName> element).
Then build, and your *.doc file will be embedded.
In your code, do this:
// use a GUID generator to create a new GUID in a string literal (do not run `Guid.NewGuid()` or `new Guid()` at runtime!) to create a globally unique filename so you don't accidentally overwrite other files
const String _myFileName = "12345.doc";
static void ShellInvokeReadmeDocument() {
String destFileName = Path.Combine( Path.GetTempPath(), _myFileName );
if( !File.Exists( destFileName ) ) {
Assembly assembly = Assembly.GetExecutingAssembly(); // change as appropriate
using( Stream source = assembly.GetManifestResourceStream("Documents.Readme.doc") )
using( Stream destination = File.OpenWrite( destFileName ) ) {
source.CopyTo( destination );
}
}
Process.Start( destFileName );
}
Note that I don't recommend using a .doc or .docx file as it is not guaranteed that the user will have Word installed (unless you know your customers will have it).
Note that WordPad in Windows does not necessarily have the ability to open .doc files! On Windows 7 and later WordPad only opens .docx files and does not support all formatting options, only a small subset, and WordPad on Windows Vista and earlier does not support opening .docx files.
If you want to maximize compatibility, I recommend using .pdf files if you want to preserve printed-page formatting and layout, or an .rtf file.
If this file is meant to constitute a Help or Documentation file, then you should use a .chm file instead, which is structured and fully supported by all Windows versions from Windows 98 onwards - you can also integrate .chm files with WinForms with the "What's this?" button and tooltips (.chm files are not to be confused with .hlp files which are not supported by Windows after Windows Vista).
I'm working in c#, and looking for a way to create a path to a directory that will map to an IO.Stream instead of to the actual file system.
I want to be able to "save" files to that path, manipulate the content or file names, and then save them from that path to a regular file in the file system.
I know I can use a temporary file, but I would rather use the memory for both security and performance.
This kind of thing exists, according to this answer, in Java, using the FileSystemProvider class. I'm looking for a way to do it in c#.
I've tried every search I could think of and came up only with the java answer and suggestions to use Temporary files.
Is it even possible using .net?
Basically, I'm looking for a way to enable saving files directly to memory as if they where saved into the file system.
so, for instance, if I had a 3rd party class that exposes a save method (save(string fullPath)), or something like the SmtpServer.Send(MyMsg) in this question, i could choose that path and save it into the memory stream instead of onto the drive. (the main thing here is that I want to provide a path that will lead directly to a memory stream).
.NET doesn't have an abstraction layer over the host OS's file system. So unless you can build your own for use in custom code, and you need to have 3rd party libraries covered, there are just two workable optilns:
Use streams and avoid any APIs working with file names.
Build a virtual file system plugged into your host OS's storage architecture; however, the effort needed versus benefits is highly questionable.
I went through a similar situation lately, and there is no out of the box solution in .NET for doing that although I used a workaround which was efficient and safe for me.
Using Ionic.Zip Nuget package you can create a whole directory with a complex structure as a stream in memory and although it will be created as a zip file, you can extract it as a stream or even send the zip file as a stream.
using (var zip = new Ionic.Zip.ZipFile())
{
zip.AddEntry($"file1.json", new MemoryStream(Encoding.UTF8.GetBytes(someJsonContent)));
for (int i = 0; i < 4; i++)
{
zip.AddEntry($"{myDir}/{i}.json", new MemoryStream(Encoding.UTF8.GetBytes(anotherJsonContent)));
}
}
And here is how to extract a zip file as a stream using Ionic.Zip
I have a tab delimited text file that I need to upload to a secure folder for SSIS to import into SQL Server. The file will be uploaded by an external user VIA a web app.
My challenge is that I need to check this file for some things before I am allowed to let it go to the secure folder. Namely I need to make sure the user has a specific column.
I can do this if I save the file to a folder in the web app. However the nature of the data in the file is such that we do not wish to place this file anywhere other then the secured folder. I also can not place it directly to this folder because the SSIS package is set to trigger as soon as the file shows up there.
What I need to do is find a way, if there is one, to parse the file in memory and if it passes all the checks, upload it to the secure folder.
I'm using C#.NET and the FileUpload Control.
My search so far has included all kinds of information but they all require saving the file somewhere first and then working with it.
Thank you so much for your time. If anybody can point me to an object or some code I can check out I would be most grateful.
Rather than calling SaveAs, use the FileContent property to access the contents of the file as a Stream, then you can do whatever processing is required before you save it manually.
For example, something like this:
string data;
using(StreamReader reader = new StreamReader(fileUpload.FileContent))
{
data = reader.ReadToEnd();
}
The data variable now contains the contents of the file as a string. You can do whatever processing you like, then save it (or not) to the appropriate location.