Is it possible to create .doc or .docx or .rtf file (any Word file) programmatically (using C#) on Windows Phone (Silverlight) 8/8.1?
I cannot find such information. Thanks.
No, it's not possible because there are no available APIs to interact with Office Hub. The other answer could not work because a .docx file is not a file contaning plain text such as .txt.
For more info about .doc files: http://en.wikipedia.org/wiki/Doc_%28computing%29#Microsoft_Word_Binary_File_Format
I think you can create it through the Isolated Storage Concept.
using System.IO;
using System.IO.IsolatedStorage;
Writing Process:-
var appStorage14 = IsolatedStorageFile.GetUserStoreForApplication();
string filename14 = "test.docx";
using (var file = appStorage14.OpenFile(filename14, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
{
using (var writer = new StreamWriter(file))
{
writer.Write("-- : -- : --");
}
}
Reading Process:-
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (StreamReader sr = new StreamReader(store.OpenFile("test.docx", FileMode.Open, FileAccess.Read)))
{
string t;
t= sr.ReadToEnd();
MessageBox.Show(t);
}
}
Related
i have a question. If there is a possibility at windows phone 8 at visual studio to create button event to read text file? i know about streamReader and if i declare wchich exacly file i want to read, but if i want to choose from list of files wchich i want to display. i did research on the Internet but i didint find an answer. I know i can use isolatedStorage to read music, video, image but not text files, on the app i created few files with text in it and i want users to have posibility to display one from this file, whichever they want to see. So, can you tell me how to do this?
You can use IsolatedStorage to read any file type you wish. You must of been using something like a Launcher that filters out the file type based on the Chooser.
You can open a file like this:
private async Task<string> ReadTextFile(string file_name)
{
// return buffer
string file_content = "";
// Get the local folder
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
if (local != null)
{
// Get the file
StorageFile file;
try
{
file = await local.GetFileAsync(file_name);
}
catch (Exception ex)
{
// no file, return empty
return file_content;
}
// Get the stream
System.IO.Stream file_stream = await file.OpenStreamForReadAsync();
// Read the data
using (StreamReader streamReader = new StreamReader(file_stream))
{
file_content = streamReader.ReadToEnd(); // read the full text file
streamReader.Close();
}
// Close the stream
file_stream.Close();
}
// return
return file_content;
}
If you want to get the PackageLocation (files that you added into the project like assets and images) then replace the LocalFolder with
Windows.ApplicationModel.Package package = Windows.ApplicationModel.Package.Current;
Windows.Storage.StorageFolder installedLocation = package.InstalledLocation;
With Windows Phone 8.1, File Pickers are allowed, consisting the same functionality you are expecting, so probably you might want to upgrade your app to WP8.1.
Here's more info on this API : Working with File Pickers
I'm writing a windows phone 8 application that have following functions
Download a zip file from the internet
Extract it to the isolated storage
I'm looking for a solution to deal with it but haven't found once. If you have any suggestion please help.
Thanks in advance!
EDIT:
I break it down into several steps:
Check if storage is available - DONE
Check if file is compressed - DONE
Use Background Transfer (or another method) to download to local folder and display information to user (percentage, ect.) - NOT YET
Unzip file to desired location in isolated storage - NOT YET
Do stuffs after that... - DONE
For step 4, I found and modified some script to extract file to isolated storage (using SharpGIS.UnZipper lib):
public async void UnzipAndSaveFiles(Stream stream, string name)
{
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var zipStream = new UnZipper(stream))
{
foreach (string file in zipStream.FileNamesInZip)
{
string fileName = Path.GetFileName(file);
if (!string.IsNullOrEmpty(fileName))
{
StorageFolder folder = ApplicationData.Current.LocalFolder;
folder = await folder.CreateFolderAsync("html", CreationCollisionOption.OpenIfExists);
StorageFile file1 = await folder.CreateFileAsync(name, CreationCollisionOption.ReplaceExisting);
//save file entry to storage
using (var writer = new StreamWriter(await file1.OpenStreamForWriteAsync()))
{
writer.Write(file);
}
}
}
}
}
}
This code is untested (since I haven't downloaded any file).
Can anyone point out any thing that should be corrected (enhanced)?
Can anyone help me to modify it to extract password-protected file (Obviously I have the key)?
I am trying to upload a Word Document to my personal box account using Box Windows SDK V2 using the following code.
using (Stream s = new FileStream("C:\\word.docx",
FileMode.Open, FileAccess.Read,
FileShare.ReadWrite))
{
MemoryStream memStream = new MemoryStream();
memStream.SetLength(s.Length);
s.Read(memStream.GetBuffer(), 0, (int)s.Length);
BoxFileRequest request = new BoxFileRequest()
{
Parent = new BoxRequestEntity() { Id = "0" },
Name = TxtSaveAS.Text
};
BoxFile f = await Client.FilesManager.UploadAsync(request, memStream)
The document gets uploaded successfully in root folder but the problem is, the extension of document is set to "File" (which is not previewed by Box because of having unsupported extension, neither it gets the icon of word document) rather than "docx" though it still gets open correctly in Microsoft word.
How to upload file using box windows sdk with respective extensions.
suggestions are greatly welcomed.
In order to upload the file with the correct extension, simply append the extension to the Name.
BoxFileRequest request = new BoxFileRequest()
{
Parent = new BoxRequestEntity() { Id = "0" },
Name = TxtSaveAS.Text + ".docx"
};
As the title said, if I don't want to open the specified file, how can I get its file size from the file attribute on windows phone? Thank you!
The easiest is to use the FileInfo class.
Here's how to do it:
FileInfo info = new FileInfo(filePath);
Debug.WriteLine("File size=" + info.Length);
I think you cannot do this without opening the file. You will have to open stream and check its Length. MSDN
EDIT - sample code added
You probably know how to do this, in case not - here is the sample code:
private long fileLength(string path)
{
long fileLength = -1;
try
{
using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
using (IsolatedStorageFileStream file = ISF.OpenFile(path, FileMode.Open))
fileLength = file.Length;
}
catch { }
return fileLength;
}
I'd like to create an application that receives formatted text (RTF) or html, renders it an show it page by page..
Is there any control that aims to do that?
I tried to use the RichEditBox control to load a file but it stucks during the operation:
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(#"myFile.rtf");
using (var memstream = await file.OpenReadAsync())
{
MainText.Document.LoadFromStream(Windows.UI.Text.TextSetOptions.ApplyRtfDocumentDefaults, memstream);
}
I tried to load an HTML file this way:
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(#"myFile.htm");
var stream = await file.OpenAsync(FileAccessMode.Read);
string app;
using (StreamReader rStream = new StreamReader(stream.AsStream()))
{
app = rStream.ReadToEnd();
}
myWebView.NavigateToString(app);
But I cannot find a way to "count" the lenght of the parsed text to chunk it in pages..
There is any other way or library to do that? Any example online?
If you want to show your HTML contents in pages then you can use RichTextBlock with RichTextBlockOverflow. RTF is not supported to RichTextBlock.
how to inject RTF file to RichTextBlock in c#/xaml Windows store app
Showing Html in WinRT with RichTextBlock or other component
XAML text display sample