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.
Related
I need to create a process that creates/modifies some text files in a folder. I am using below code to do that:
file = new System.IO.FileInfo(filePath);
file.Directory.Create();
System.IO.File.WriteAllText(file.FullName, "Some text...");
I have a Biztalk queue that looks into the text files in the folder every 2 minutes and picks up the files to process them. I want to lock the files when I am creating/modifying so that Biztalk wont try to process those files. How can I achieve this?
I read about Transactional NTFS in windows which will let me create Transaction context but windows documentation says this feature will deprecated and recommends not to use it.
If the file is on a local NTFS volume of CIFS share, the File Adapter will not attempt to read an open file. However,
A better pattern would be to do your file work in a temporary folder, then copy the completed files to the BizTalk folder only when they are done. That way, you don't have to worry about locking at all.
To acquire an exclusive lock you can use the file stream to do so
using (FileStream fs = new FileStream("Test.txt", FileMode.Append, FileAccess.Write, FileShare.None))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine("test");
}
}
This way you are locking the file exclusively for the current file stream. Any other application or even a new instance of file stream from another thread within the same application attempts to read or write to the file will be denied by the operating system.
In most cases, write file with different extension then rename the file works fine.
I try to copy a file that is included in the app bundle as a resource to the temp folder on the iPhone. While this works on the Simulator, on the device I get an exception:
System.UnauthorizedAccessException: Access to the path
"/private/var/mobile/Applications/B763C127-9882-4F76-8860-204AFEA8DD68/Client_iOS.app/testbundle.zip"
is denied.
The code I use is below It cannot open the source file.
using(var sourceStream = File.Open("./demobundle.zip", FileMode.Open))
{
sourceStream.CopyTo(targetStream);
}
What is the correct way of copying a file into a destination stream?
Why is it that I always find the answers to my questions practically immediately after I asked here? :-)
One has to specify the file access mode. If it is set to Read, it'll work. The default seems to be some write mode and that is obviously not possible.
using(var sourceStream = File.Open("./demobundle.zip", FileMode.Open, FileAccess.Read))
{
sourceStream.CopyTo(targetStream);
}
I'm new to C# and would appreciate a little help.
I've a windows console application that emails logs and is scheduled to run every hour.
What I want is to zip those logs(after they have been emailed) in the same folder. ie. the folder the application is reading the logs from.
This is snipped of my code so far.
string[] files = Directory.GetFiles(#"C:\Users\*\Documents\target", "*.txt");
try
{
using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile()) //i'm using dotnetzip lib
{
foreach (var file in files)
{
Console.WriteLine(file);
sendEMail(file);
zip.AddFile(file,"logs");
}
zip.Save("mailedFiles.zip");
}
}
What's happening with the above code is I'm able to create a zip file but not in the same folder where the application is reading from. Instead it creates the zipfile in my program's location(which makes sense).
How do I go about changing the location of the created zipfile. Also I want the individual logs to be replaced by the one zipfile that's created.
You can save the zip file to any of the special folders available in the user folder. You can get paths for the special folders with the following line of code:
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
If you want to save the zip file other than these special folders then there might be permission issues.
Try saving it by using the directory.
For example:
{
string directory = #C:\Users*\Documents\
zip.Save(directory + "mailedFiles.zip");
}
You should also use System.IO to get the directories instead of hardcoding them.
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.
Im trying to clone facebook image uploader which is built in java. But I would like to use silverlight so Im wondering if I can somehow read local directory.
If I have this running an some remote server I can easily read the content of that server as I have C# as backend. But Im not sure how could I read certain directory of the user which is using silverlight application.
Any ideas if this is possible or not?
It's possible to read file "blindly" using OpenFileDialog. Blindly means you can let the user point the dialog to the file so Silverlight can read its content but it can't tell where the file is located.
Example:
var fileDialog = new OpenFileDialog();
var dialog = fileDialog.ShowDialog();
if (dialog.HasValue && dialog.Value)
{
byte[] bytes;
using (var fileReader = fileDialog.File.OpenRead())
{
bytes = new byte[fileReader.Length];
fileReader.Read(bytes, 0, (int) fileReader.Length);
}
}
The access to the file system is limited for security. Some access (blind as well) can be done using Isolated Storage where you can store data and access later.