FileSavePicker: Saving to memory? - c#

I have a WinRT Metro app (XAML/C#, Windows Release Preview) that manages documents. The documents are represented in-memory and fetched via a Web Service, but are not physically stored on disk.
To improve my app I wanted to implement the FileSavePicker contract. Thus other apps could save their documents directly into my app. But it doesn't seem possible to get the content from the file save picker in memory into my app. I have attached an event handler to the FileSavePickerActivatedEventArgs.FileSavePickerUI.TargetFileRequested event that is fired when the user wants to save a file to my app. But here I have to set TargetFileRequestedEventArgs.Request.TargetFile to a StorageFile and since I'm in memory and don't have a file yet, this action fails.
Furthermore (even if I would have a StorageFile) I have to react on the completion of the file saving process and there seems to be no method/event that I could take to handle this.
I don't understand why Microsoft decided to go this API way for the file save picker contract. Why can't I get the saved data/file as parameter/stream and do what I want? Sandboxing/security can't be the reason in this case!
Edit: I've seen and looked at the file picker contracts sample app from the SDK, but it doesn't help, since it only saves to a physical StorageFile.

Have you looked at the CachedFileUpdater contract? You can use this to receive notifications when the source app makes writes to the file. You would still need to create a StorageFile for this.

You need to use StorageFile.CreateStreamedFileAsync. This method will return to you a StorageFile that is built on the fly by you when data is requested from it. The delegate you pass in the second argument will be invoked when data is requested from the "file". When this happens, you can pump in data from any stream you want.

Related

C# Monitor File Activity

I need to monitor a file for changes and see exactly where it has been modified.
It seems like I can't use FileSystemWatcher as I only get notified that a file has been modified but I can't know exactly what changed, and storing copies of the file to do a diff after the event is raised is not feasible since the file is really big.
I would like to implement something like what SysInternals have on their Process Monitor software, that tells us, for a WriteFile event, the modified part of the file given an offset and length.
I read that there is a Windows API that does this, but I couldn't find anything on how or where to start implementing it on a .net 3.5 application.
From what I have investigated, the .NET platform provides this event for handling file change events. However, this alone will not let you know what part of the file has changed.
You will need to store somewhere a copy of the file that is compared to the file whenever it changes, and then update that copy when the comparison is done. This is a very basic solution and can get really inefficient if the file is too large.
Using the FileSystemWatcher you can subscribe to the changed event ChangedEvent
FileSystemEventArgs contains a property FullPath which can be passed in to FileInfo. This will give you some basic file information, which can be stored to a database for example. You can keep a history of the file changes this way.
If you need to compare the content of the files it is more challenging and the approach will be different depending on file type.

What is the File path to specify to transfer a file from Android to Windows PC?

I am going to create an c# windows application for transfering image file from an android phone to my Wondows PC - when I connect phone with my PC(using data cable). When I given the path "Computer/Nuxes5/..." in C# for accessing files from mobile, (Got from the windows Explorer address bar). Then getting incorrect path. Following is the code I have given for accessing files.
Directory.GetFiles(#"Computer/Nuxes5/...");
Can any one please suggest me, how to access the mobile files using C#.
You need to use MTP file transfer. Since you are using Windows, the best thing to do is to use COM with the Windows PortableDeviceApiLib library. This is not an easy task. The WPD API link in one of the comments above is a good reference.
You should also install Microsoft MTP Simulator 3.0 and look at the sample code that comes with it.
In MTP, every file or folder stored on the device is an object with a handle. To retrieve a file or a folder, you have to retrieve the object handle, then check to see if it is a file or a folder by checking its objectFormatCode property. Folders have the object format code set to 0x3001. You can get the entire list from the MTP Spec.
Once you have the WPD/PTP wrapper set up, you can start sending MTP commands to the device. For getting files from the device, the procedure is the following.
Get the available storage ids by calling getStorageIds();
For the storage id you are interested in (internal storage/SD card), call getObjectHandles() to get a list of all files/folders.
Loop through the root folder handle to look for the file you are interested in. For each handle you get, call getObjectInfo() to get the details about that handle.
Once you have a handle whose name and format code matches what you are looking for, call the getObject() function to download the file.
Also remember that you cannot download all the contents at the same time. You have to call getObject() for each file handle you need to download.

Perform a file-upload action within WinRT

I spent some reading to figure out how to perform a complete file upload process with Windows RT. The "circumstances" regarding StorageFolder, StorageFile, security box model and such stuff is (at least known, but well known) subjects. Perhaps I started at wrong end, but I hope experts out there can help this question a meaning.
I have a
- Windows 8 App Store project.
- WinRT Business DLL Project.
- WinRT Unit Test Project.
This will keep me sure that i work with WinRT related objects.
The question is:
How do I perform a file stream from my test project to logic in Business DLL project? So I can i.e. stream the uploaded file to application temp folder?
As a paranthese. From within the Windows 8 App Store Project I'm honestly also try to success let the FileOpenPicker give me the stream, not just the filename. The question attached below added some useful information. Though i feel it's more verbose then needed?!
save stream to file in c# and winrt
From you test project, you can use FileIO.ReadBufferAsync to get data into an IBuffer. Once you have that, you can read or write it with a DataReader or DataWriter as you please. Check out this example on how to manipulate the data with readers and writers.

Writing a Dynamically built HTML to a file in ASP.NET 4.0

I have a web form which takes in user information. The value of various text boxes is used to build a html file. I write this html to a file( with specific name) and then prompt user to Save this file.This html is used for creating outlook email signatures. Currently I have this html within the application.This has been deployed to the server. I had to set write permission on this file for all users for it to work.
Are there any security risks? What happens if multiple users access this applications and write to the file at the same time.
When you say the file has "a specific name", do you mean that it is always the same name? If so, then yes, there will be problems if multiple users use this functionality at the same time. They'll be overwriting the one file and downloading each other's data. You would need to generate a unique filename each time the process runs to avoid this.
But do you actually need to save the file?
Or is your goal purely to produce some HTML for the user to download, and the way you are doing this is by writing it to a file, and then prompting them to download that file?
If you don't need to save the file, but rather just need to generate HTML and prompt the user to save, just serve it up as a normal page, and set response headers such that their browser will download it. Something along these lines:
Response.AddHeader("content-disposition", "attachment;filename=my_file.html");
From what I understand, the user fills the web form and submits. Immediately, an html file pops up for download from the server. I think this is very neat implementation of this scenario. You just need to make sure that resources are released properly in order to prevent locking of files.
When multiple users access this application, it should not break since separate files are created with a specific name (as you have mentioned). I don't know what logic has been used to create unique names. At some peculiar situation (this is purely dependent on your name calculation logic) if the calculated specific file name somehow becomes similar to an existing file, you should have code in place to replace or create a different version of the same file name. Locking could occur if you are writing captured data from web form into the same file again and again without disposing your stream/File objects . Make sure you dispose your objects after use in the code.
It would be great if you give access to the application pool of the web application to a user who has write access to that file/folder instead of giving everyone the write access. In this way, your application gets full rights to perform write operations rather than users having rights. If users have write access on the file/folder, it is very easy for anyone to peek in and do something unexpected.
Hope this helps.

Reading a Bitmap from a TIF file - Works in WinForms not in ASP.NET

EDIT Solution Found: See my post below.
We are writing a library that reads in a TIF file from a scanner. Basically, its a scantron. We are examining the form and reading values from it.
Currently we have a windows test app, and we give it a filepath as a string ("c:\testing\image.tif"). It loads up the tif, reads the document correctly and parses the values.
We also have an ASP.NET web application. We have a test page that does exactly what the windows app does, we hand it an identical string, and i calls the same function on the same class from the same library. It however does NOT read the form correctly. We have verified that it does it fact load up the tif file, and it is actually filled with data (pixels we expect to be white/black are white/black when we examine the Bitmap obect in the immediate window of Visual Studio).
The specific problem is in a library called DataMatrix we use to scan a bar-code off the document. This function is supposed to return a List<string>, each of which is a barcode the library found on the document. In the windows app, this function (DataMatrixDecoder.DecodeBarcode(bitmap)) correctly returns with a Count=1. When using the asp.net app, this returns with Count=0.
Because its the exact same image file, I cannot imagine the problem is in DataMatrix. I can only assume its something with ASP.NET or something.
This isn't even my project, but another guy and I are helping our coworker figure this out, and we are just pulling our hair out. All signs indicate that ASP.NET is correctly loading and handing the image off disk to the "processor" class (which is a class library that uses the DataMatrix stuff, we are not doing ANY code in ASP.NET except for opening/handing the file to the function.).
Does anyone have any ideas as to what it might be, or different things we can check?
I'm not even sure what kind of information to give so I tried to say it all, if you have any questions please ask I'd be more than happy to elaborate on anything. Thanks.
edit:
this is the code on the ascx.cs code-behind, in a button-click event:
if (formReader.ReadTIFF(#"c:\testing\image.tif"))
{
messages.Controls.Add(HtmlHelper.DivSuccess("Read successful."));
}
The formReader class then open the file with a FileStream, and uses that to create a Bitmap. The ASP.NET application is not actually opening the file at all (we were uploading it through a FormUpload control, but after experiencing problems we dummied it down to this). This is the most perplexing thing, that it works in the windows app but not from this web site. ASP.NET has full permissions on that folder to do whatever it wants. It can open the image fine, and the bitmap it creates from the FileStream is the actual image.
edit: Also, the ReadTIFF function right now copies the FileStream into a MemoryStream, ensuring its a not a problem streaming from disk (the entire file is in memory).
How are you passing the filepath to the web application?
It is possible that the function which Decodes might be swallowing some exception.
Use reflector to examine the library (if you have not written it).
I agree. It seems your problem is most probably related to User rights on the directory where you're trying to access the files from. Try giving your Web users the Full access rights on the source directory.
EDIT
Solution Found: The problem was that the open file dialog was changing the CurrentWorkingDirectory. The reason the website never worked, was because the Environment.CurrentDirectory was set incorrectly. When I manually set the CurrentDirectory to the websites' bin folder, parsing works correctly.
Small update. Using the Windows App, and selecting the file via OpenFileDialog, will cause the barcode decoder to fail. Technically, I am using the exact same string to hand to the parser ("c:\testing\image.tif"), yet when I use the OpenFileDialog to get the string, the decoder fails. Is there a clue in this?
update: In fact, even if I don't use the string the OpenFileDialog gives me, if I just open the file dialog at all, it will fail. I don't get this. It's something simple. I need to debug the C++ DataMatrix library, really.

Categories