add resourse files to project with code - c#

How could I archive this:
programmatically?
Is there a way of creating resource files with code?

I think you can't watch in resources but you can use resource.
FileStream fs = new FileStream("Resources",FileMode.OpenOrCreate, FileAccess.Write);
IResourceWriter writer = new ResourceWriter(fs);
writer.AddResource("TextFile1", "SomeText");
writer.Generate();
writer.Close();

Related

Path of XML file in Resources folder to FileStream in C# WPF project

In a simple WPF project I have an XML file-"Students.xml" to view and add records. XML file is located in project's Resources folder with properties Build Action : Resource and Copy always. I can read the XML using:
XDocument doc = XDocument.Parse(Properties.Resources.Students);
But for writing to it, FileStream requires its path in string.
FileStream fs = new FileStream(Properties.Resources.Students, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
// exception in first argument, string path should be specified.
So, how should I access xml file from resource folder in FileStream. Please help.
Problem solved! I just added the file in the project and also copied that file in project's debug folder as well. Now I can simply access "filename.xml".
XDocument doc = XDocument.Load("Students.xml");
/...../
FileStream fs = new FileStream("Students.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
Now,its working.
If you are using a Resource File, then its contents are embedded into the assembly, so there is no file path for them. Instead, you should add a folder named Resources into your project and add the XML file in there. If you do that, then you can access it using this property:
public static string ResourceFolderPath
{
get
{
string path, applicationDirectory = Path.GetDirectoryName(
Assembly.GetEntryAssembly().Location);
if (applicationDirectory.Contains(Environment.GetFolderPath(
Environment.SpecialFolder.UserProfile))) path = Path.Combine(
applicationDirectory, "Resources");
else path = Path.Combine(Directory.GetParent(applicationDirectory).Parent.
FullName, "Resources");
return path;
}
}

Opening .xlsx Files In Npoi

I am trying to open an .xlsx file using Npoi but it keeps crashing with the following error:
1 is not a supported code page.
Parameter name: codepage
My code is very simple:
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Excel Workbook|*.xlsx";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
XSSFWorkbook myWorkbook;
FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
using (fs)
{
myWorkbook = new XSSFWorkbook(ofd.FileName);
}
}
The error happens while trying to create the workbook. I tried also using the stream, such as:
myWorkbook = new XSSFWorkbook(fs);
Does anyone know what is wrong? I can't find a proper example on the net for dealing with .xlsx files. I am suing the latest build (2.0.1).
Thanks.
ICSharpCode.SharpZipLib.Zip.ZipConstants.DefaultCodePage = Encoding.Default.CodePage;
...
FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
XSSFWorkbook workbook = new XSSFWorkbook(fs);
it works for me... ;)
I have been using the Workbook Factory without issues. It will detect whether the file is xls or xlsx and return the appropriate object for you. Note that this is version 2.06.
A quick sample:
_fileStream = new FileStream(filenamePath, FileMode.Open, FileAccess.Read);
_currentWorksheet = _workbook.GetSheetAt(0);
_workbook = WorkbookFactory.Create(_fileStream);
_fileStream.Close();
I have been Apache POI user for the last decade and I thought that NPOI was as good as his Java father but I'm afraid TheGateKeeper is right: a long way to go.
I have to look for OpenXML :(
I was able to open the file successfully using EPPlus, another Excel library. I still use NPOI for .xls files but for .xlsx I think it has a long way to go.
Please try the latest NPOI version: NPOI 2.0 RC. Here is the link: https://npoi.codeplex.com/releases/view/112932

File is used by another process: How to solve this Error?

I am trying to open a file, but I received:
The process cannot access the file because it is being used by another process. The File is an XML-Document. Can anyone help?
string activeDirectory = #"X:\SubGraph\";
string[] files = Directory.GetFiles(activeDirectory);
foreach (string fileName in files){
FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read);
After using a file, you must to close it, I think:
foreach (string fileName in files)
{
FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//your code
file.Close();
}
If you are using this piece of code in some kind of loop you need to close your FileStream each time before finishing loop cycle.
file.Close();
Or use "using" construction like this:
using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
// your code goes here
file.Close();
}
Moreover, you better to accustom yourself to close all manually created streams after they are unnecessary anymore.
Under some circumstances Windows locks the files. In your case can be:
Another process is locking the file. It might be windows or you av software or who knows.
In order to discover who is locking the file you might several tools like wholockme or Unlocker. These tools will tell you which process is locking the file and even allow you to unlock it.
Maybe you are locking your own file. In your code snippet seems you are not closing the file you are reading (Maybe you can edit your question and add all code). You should remember to include:
file.Close();
... or file will remain open.

StreamWriter and IsolatedStorageFile

IsolatedStorageFile iF = IsolatedStorageFile.GetUserStoreForApplication();
if (!iF.DirectoryExists("aaa"))
{
MessageBox.Show("No directory, create!");
iF.CreateDirectory("aaa");
}
StreamWriter fW = new StreamWriter(new IsolatedStorageFileStream("girls\\list.txt", FileMode.OpenOrCreate, iF));
fW.WriteLine(this.tb_name.Text);
So, I create file, or open it, and add to it content of textbox. I need append this file, but it rewrites. Please, help me to solve this problem :) Thank you!
You want FileMode.Append, not FileMode.OpenOrCreate
See this page for details http://msdn.microsoft.com/en-us/library/system.io.filemode(v=vs.95).aspx
Append: Opens the file if it exists and seeks to the end of the file, or
creates a new file.
Use FileMode.Append for if it exists, FileMode.Create if it does not.

Rename File open by self

My program is logging data to a file, at the same time a user interface displays the incoming data live. I want the logged data to be on disk within a second or two if computer/program/os/whatever shuts down. Data is coming in at least 100 times/sec.
I want the user to be able to give the log-file a new name, while logging is active. The problem is that i can't change the name of the file while it is open, even if it is by the same process.
Test case:
string fileName1 = "test.txt";
string fileName2 = "test2.txt";
using (StreamWriter sw = new StreamWriter(new FileStream(fileName1, FileMode.Create)))
{
sw.WriteLine("before");
File.Move(fileName1, fileName2); //<<-- IOException - The process cannot access the file because it is being used by another process.
w.WriteLine("after");
}
So, How do i rename a file from a process while the same process is having a stream to the file open?
You should close the first stream, rename the file, then reopen the stream:
using (StreamWriter sw = new StreamWriter(new FileStream(fileName1, FileMode.Create)))
{
sw.WriteLine("before");
sw.Close();
}
File.Move(fileName1, fileName2);
using (StreamWriter sw = new StreamWriter(new FileStream(fileName2, FileMode.Append)))
{
sw.WriteLine("after");
}
I know this answer is a bit late to help you on your porting project but perhaps it will help others!
If you open the file with the FileShare.Delete flag it will let you rename it even though it is still open :)
You can't rename a file while it is open by a process, but if you want to write to it from the other instance of your program, do this.
Try FileShare.Write. You can use it in File.Open.
using (StreamWriter sw = new StreamWriter (File.Open(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
{
...
}
Opening en closing the file 100 times a second will have a impact on your performance, you can log to a temp file and append the temp file every 10 seconds or so. That will give you what you want.

Categories

Resources