Problem copying contents of virtual machine with DiscUtils - c#

I'm able to view a virtual machine image (vhdx) using the Discutils library (https://github.com/DiscUtils/DiscUtils)
There are files belonging to the Linux operating system in these Vhdx
How can I transfer these files to the windows environment because of the special characters in their names?
What came to my mind was to remove the special characters in the filenames. However, since the method I use wants to open the file first, windows does not allow it.
var sourceStream = fs.OpenFile(name, FileMode.Open, FileAccess.Read);
var targetStream = System.IO.File.OpenWrite(#"D:\" + name);
sourceStream.CopyTo(targetStream);

Related

Access Denied When Creating a new File Using Filestream - Xamarin IOS

I am trying to create an empty PDF file in Xamarin.IOS using Filestream. When I try to create the Filestream using the following code:
FileStream fs = new FileStream("InvestmentAgreemen.pdf", FileMode.Create, FileAccess.ReadWrite);
I get the following error: "Access to the path "/private/var/containers/Bundle/Application/05AA1616-B66B-483D-8BA1-80A2B1AEC973/NewEPA.app/InvestmentAgreemen.pdf" is denied."
It worked fine when running on the iPad simulator, but as soon as I moved it to a real device I got this permissions error. Additionally, I have to use a filestream to create the empty PDF because I am also using Syncfusion's PDF tools for xamarin.IOS which require the use of Filestream for saving.
I also tried creating a FileIOPermission object granting AllAccess to AllFiles but I got a system not allowed error.
How can I resolve this permissions error?
You are trying to create a file in the root of your application's bundle, which is read-only. I'm surprised that this didn't fail on the simulator also.
To create a file at runtime, you need to specify a user writable path
var documents =
Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
var filename = Path.Combine (documents, "InvestmentAgreemen.pdf");
Create your pdf within a writable directory of your application instead of the root directory of the bundle:
using (var docPath = NSFileManager.DefaultManager.GetUrl(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomain.All, null, true, out var nsError))
using (var fileStream = new FileStream(Path.Combine(docPath.Path, "InvestmentAgreemen.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
// do something with the file stream
}
I have created a sample which creates and saves the PDF document in Xamarin.IOS platform. Which can be downloaded from the below location:
http://www.syncfusion.com/downloads/support/directtrac/184238/ze/PDFSave1731207096
Please try the sample and let me know whether the issue is fixed.
Regards,
Surya Kumar

Folder to store data files locally in WPF application

I currently have the code below in my WPF application which does exactly what I want it to do, however, upon publishing this it won't necessarily be able to access these folder locations as they won't be pointing to the correct directory nor will the folders exist.
I was hoping somebody might be able to tell me what is the best way to save something into a local folder?
Whether it's inside the application folder itself or not is of no issue either.
The code I'm currently using for the writing of the file:
using (Stream stream = File.Open(#"..\..\Templates\data.bin", FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(stream, templateList);
}
The code I'm currently using for the loading of the file:
using (Stream stream = File.Open(#"..\..\Templates\data.bin", FileMode.Open))
{
BinaryFormatter bin = new BinaryFormatter();
templateList = (List<Template>)bin.Deserialize(stream);
}
You could use System.Environment.SpecialFolder.LocalApplicationData to store application specific data:
using System;
class Sample
{
public static void Main()
{
Console.WriteLine("GetFolderPath: {0}",
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
}
}
Ref: http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx
You can use Environment.SpecialFolder to find an appropriate place to put files (for example, ApplicationData would be a good place to start). If you only need a temp file, you can use Path.GetTempFileName to create one.
Edit: One last note. Storing stuff in the application folder itself can be a giant pain. Usually the application folder is created with the admin account during the installation so your app won't be able to write to it while running on a user account.

C# : edit file access control

hi i am working on c# project and i try to lock a file from being opened , copied or even deleted by using that code :
FileInfo fi = new FileInfo(textBox1.Text);
FileSecurity ds = fi.GetAccessControl();
ds.AddAccessRule(new FileSystemAccessRule("Authenticated Users", FileSystemRights.FullControl, AccessControlType.Deny));
fi.SetAccessControl(ds);
but when i open the file , it is opened and can be deleted , is there anything wrong on my code ?
by the way , that code works perfectly on anywhere but flash drive , i can block editing or copying files from computer , but on flash drive the application is useless .
What filesystem does your flash drive have? I'm guessing FAT32, rather than NTFS.
FAT32 has no concept of per-file ACLs (or as far as I know, no concept of ACLs whatsoever).
See this article:
http://technet.microsoft.com/en-us/library/cc783530(WS.10).aspx
On a FAT or FAT32 volume, you can set permissions for shared folders but not for files and folders within a shared folder. Moreover, share permissions on a FAT or FAT32 volume restrict network access only, not access by users working directly on the computer.
The only option will be to open the file in exclusive access mode to prevent others from changing it while you are reading it.
See this question (stolen from Vitaliy's comment):
How to lock file
The code from the accepted answer:
using (FileStream fs =
File.Open("MyFile.txt", FileMode.Open, FileAccess.Read, FileShare.None))
{
// use fs
}

C# write to ext2 linux partition from Windows

I'm quite new to C# and I need to write a file (grub) on an EXt2 linux partition from windows 7.
What is the good way to do such thing? Do I need to mount the partition with external program?
I think you need to mount it with an external program such as: http://www.fs-driver.org/
Mount the drive using a a driver like FS-driver and then write to it using standard C# file writing techniques.
You can use Ext2Fsd to mount the partition in windows, and then write to as you would any other partition.
EXT2FSD Home Page
If you use Total Commander under Windows, one of the available plugins is for accessing ext2/3/4. It's probably already been mentioned here but it makes accessing Linux from Windows almost transparent. I don't have it installed right now or I'd look at the name.
SharpExt4 may help you with Linux file system read and write.
A .Net library to provide full access (read/write) to Linux ext2/ext3/ext4 filesystem
Here is the GitHub link
https://github.com/nickdu088/SharpExt4
//Open EXT4 SD Card
//Here is SD Card physical disk number. you can get from Windows disk manager
ExtDisk SharpExt4.ExtDisk.Open(int DiskNumber);
//In your case FAT32 is 1st one, ext4 is 2nd one
//Open EXT4 partition
var fs = ExtFileSystem.Open(disk.Parititions[1]);
//Create /home/pi/file.conf file for write
var file = fs.OpenFile("/home/pi/file.conf", FileMode.Create, FileAccess.Write);
var hello = "Hello World";
var buf = Encoding.ASCII.GetBytes(hello);
//Write to file
file.Write(buf, 0, buf.Length);
file.Close();
How to use SharpExt4 to access Raspberry Pi SD Card Linux partition

Re : Saving downloaded Files into MyDocuments

I have a doubt from a silverlight application we can access MyDocuments. I am creating an Application which will download a set of files from a remote server . Is it possible to save these file in MyDocuments instead of Isolated Storage. I am using Silverlight 4.0 . Can any one give me Sample codes for it.
In order to acheive that you need to use Silverlight 4 and specify that is should get elevated privileges when install as an Out-of-browser application. When running as an OOB the app will have access to the users Documents folder.
In all other cases you will need to use the SaveFileDialog where the user can explictly specify where to save the file.
Edit code example:-
if (Application.Current.HasElevatedPermissions)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
path = Combine.Path(path, "MySaveFile.dat");
using (var filestream = File.OpenWrite(path))
{
// pump your input stream in to the filestream using standard Stream methods
}
}
No Isolated storage is currently the only option.

Categories

Resources