Perform a file-upload action within WinRT - c#

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.

Related

storing ntfs file metadata for retrieval via webserver and linking with database

I am looking for a few pointers to reaching am adequate solution to a problem/feature I need to implement/rectify in my asp.net mvc application.
My application is a LAN only interface that is run over a webserver. In this application there is a page that displays a bunch of files/folders.
I need to be able to store a set of attributes\properties about these files, and those props\attrs need to be independent of their location on the fileserver. This is my main issue, as I could easily link them to the db with the path as the primary key, but alas then as soon as the file moves their link to the db would be lost.
The types of files that need to be displayed unfortunately could be anything. .txt, .exe, media etc etc. So that provides a limiting option also from using something like the tagsharp lib.
One approach i was considering was simply storing a key somehow in the file itself, or with an ADS ( i have no experience in doing this, but am presently trying to research its potentiality).
Does anybody have any experience with this issue, and can recommend a simple approach. I am hoping i do not need to implement an ADS approach as what ive been reading so far is a little bit over my head and im not sure C# will handle the streams adequately for my needs.
Opinion based. Proposal anyway: what about an additional file which is found by a naming convention?
MyDocument.doxc
MyDomument.docx.properties
MyMovie.mp4
MyMovie.mp4.properties
When moving / renaming files, make sure you move / rename the properties file the same.
First of all thanks for taking a moment to reply.
I had considered the possibility of using a separate file. The problem is that the users of the filesystem ( which may or may not include users of the lan application ) need to be able to move/copy files independent of db application.
Therefore if a user moves a file in windows explorer, I need it to automatically move those additional properties with it. Unfortunately I cant rely on users to move those additional files on their own volition, and I cant ask users to only use the application to move files ( if i were to generate code for the program to do this ).

visual c# button opens file without specifying drive

I work for an IT company where we all carry around flash drives that have our most used programs on them.In my spare time I am hoping to create a "main menu" item that is kind of a fun and convenient way to access these files. I am working on creating this using Visual Studio 2013 and using visual C# windows forms. I have come across a snag however that I can't seem to find a workaround for. I am by no means fluent in C#, but I need to have a button on the windows form open a file without specifying what drive it comes from. I understand that I have to specify a path, but as these will be stored on the flash drives of myself and my coworkers I cannot foresee that the path will always begin with E:. Depending on what USB slot the drive is plugged into it could be N: or F: or the like. I have provided an example below:
Using what I currently know I am opening files using this line of code:
System.Diagnostics.Process.Start("C:/Users/Myname/Desktop/Asmodeus/Anti-Virus/Anti-Virus Installers/avast_free_antivirus_setup.exe");
Is there any way possible I can have the file open simply from
System.Diagnostics.Process.Start("Asmodeus/Anti-Virus/Anti-Virus Installers/avast_free_antivirus_setup.exe");
or something of that nature?
Thanks in advance.
There must have been some mis-communication when I asked my question previously. what I am looking to do is open an executable file via a button click on the windows form using a relative path. I am not able to specify the absolute path because the application will be run from a flash drive and therefore will change depending on what USB slot it is currently inserted into.
What I am hoping to accomplish is insert a line of code that will allow me to open an executable file that is located in the \bin\debug folder along with the application itself. I have a picture for clarification but apparently do not have enough reputation to post it. Thank you and sorry for the earlier confusion.
Usually you can just use Environment.GetFolderPath (MSDN) to give you what you need. It doesn't do absolutely everything, but if you need Desktop and the like, that is plenty.
Depending on the target version of .Net, the SpecialFolders exposed are not all there. It may turn out that you need more than they provide, but in your case it doesn't sound like it.
If there is more you need that is not covered in the default, check out this project. I'm sure there are others like it, but it does a little more than the default BCL version, using the API directly. It is at least something to read and learn (and translate from vb.. use an online translator, very quick). I haven't looked at it, but it seems like you are learning this c#/.net thingy, so it might be helpful
This article is about accessing Windows special folders.
These folders include your “Favorites”, “Cookies”, system libraries and the like.
Here is code, including a large number of constant definitions, plus documentation,
allowing access to and creation of these folders.

FileSavePicker: Saving to memory?

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.

WinRT: App to enumerate files outside libraries and known folders

I am working on a Metro app that shows the content of a given folder in a ListView control.
MS decided that developers don't need the System.IO.Directory class and removed it entirely from the framework.
I am looking for a replacement to enumerate files in C# in a metro style app. I have checked all the enumeration samples provided by MS and they all seem to only enumerate the Windows Libraries using the KnownFolders class, something like:
StorageFolder picturesFolder = KnownFolders.PicturesLibrary;
and calling the GetFilesAsync() or GetFoldersAsync() methods depending on your needs. These are all gold if I wanted to enumerate only inside the pictures or music library. However I am looking to enumerate files on directories that are not included in a library.
Anyone knows how this is possible in WinRT???
You are, by design, extremely limited in this area for Metro apps. The idea is that a Metro app is only given access to those things that it is trusted to access, so you can either:
access local storage, specific to your application
access a handful of well-known storage locations, or
access a specifically granted location.
Take a look at http://msdn.microsoft.com/en-us/library/windows/apps/hh464959.aspx to get an idea as to what you'll be able to access.
From http://tirania.org/blog/archive/2011/Sep-15.html :
When you use C# and VB, you are using the full .NET framework. But
they have chosen to expose a smaller subset of the API to developers
to push the new vision for Windows 8.
And this new vision includes safety/sandboxed systems and asynchronous
programming. This is why you do not get direct file system access or
socket access and why synchronous APIs that you were used to consuming
are not exposed.
Now, you notice that I said "exposed" and not "gone".
What they did was that they only exposed to the compiler a set of APIs
when you target the Metro profile. So your application will not
accidentally call File.Create for example. At runtime though, the CLR
will load the full class library, the very one that contains
File.Create, so internally, the CLR could call something like
File.Create, it is just you that will have no access to it.
This split is similar to what has been done in the past with
Silverlight, where not every API was exposed, and where mscorlib was
given rights that your application did not have to ensure the system
safety.
You might be thinking that you can use some trick (referencing the GAC
library instead of the compiler reference or using reflection to get
to private APIs, or P/Invoking into Win32). But all of those uses will
be caught by AppStore review application and you wont be able to
publish your app through Microsoft's store.
You can still do whatever ugly hack you please on your system. It just
wont be possible to publish that through the AppStore.
So there's probably no official way, and if there's an unofficial way, it probably won't be accepted to the app store.
Just in general this makes sense: I don't want to download a seemingly legit application just to have it scan my hard drive and find my "budget.xls" spreadsheet which includes my banking/credit information.
EDIT: it is possible to grant temporary access to secure files/folders through WinRT's file picker, but it has to be invoked and chosen explicitly by the user.
You can use the StorageFolder.GetFolderFromPathAsync Method to get a StorageFolder instance from a path.
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(#"C:\...");
Note that you may not have permission to do this for all paths on your machine though.
Similar situation. Wanted to access chrome bookmarks file to parse. Had to use FileOpenPicker initially, but the file that it returns can be "cached" in the futureaccesslist(?) for subsequent retrieval.

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