I have created a full project which works perfectly. My problem concerns the setup project. When I use it on another computer, the text file cannot be found even if they are inside the resource folder during the deployment!
How can I ensure that my program will find those text files after installing the software on another computer!
I have been looking for this solution but in vain. Please help me sort this out. If I can get a full code that does that i will be very happy!
FIrst set the build action of the text file to "EmbeddedResource".
Then to read the file in your code:
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "AssemblyName.MyFile.txt";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
using (StreamReader reader = new StreamReader(stream))
{
string result = reader.ReadToEnd();
}
}
If you can't figure out the name of the embedded resource do this to find the names and it should be obvious which your file is:
assembly.GetManifestResourceNames();
This is assuming you want the text file to be embedded in the assembly. If not, then you might just want to change your setup project to include the text file during the installation.
Assuming you mean that you have a file in your project that you've set as an EmbeddedResource, you want
using (var stream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream(path))
{
...
}
where path should be the assembly name followed by the relative path to your file in the project folder hierarchy. The separator character used is the period ..
So if you have an assembly called MyCompany.MyProject and then in that project you have a folder Test containing Image.jpg, you would use the path MyCompany.MyProject.Test.Image.jpg to get a Stream for it.
create this function to read whatever embedded resource text file you have :
public string GetFromResources(string resourceName)
{
Assembly assem = this.GetType().Assembly;
using (Stream stream = assem.GetManifestResourceStream(resourceName))
{
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
Related
I saved some batch file as a resource on my application.
I want to access this file on run time - so I trying to file this file on the Resource folder but I get an exception that the
"resource folder is not there"
I trying to find the resource file by this code
var allBatchFiles = Directory.GetFiles( string.Format( #"..\..\Resources\" ) );
So how to make this work ?
Note that, when you run your application in Visual Studio, it is executed from the bin subfolder, which changes relative paths.
However, if you want to embed the batch file into your application, you are entirely on the wrong track. The resource is compiled into your EXE, and you need to use a different method to retrieve it. The following MSDN article gives an example on how this can be done:
How to embed and access resources by using Visual C#
There are at least two types of resources you might be referring to.
First, if you are referring to a RESX file, then usually you can access resources directly. So if you have a RESX file called "MyRes.resx" with a resource in it called "MyString" then you can use:
string contents = Resources.MyRes.MyString;
If you are adding files to the solution and marking them as Embedded Resources, then you can use Assembly.GetManifestResourceStream to access the data. Here's the utility functions I use:
public static Stream GetResourceStream(string pathName, string resName, Assembly srcAssembly = null)
{
if (srcAssembly == null) srcAssembly = Assembly.GetCallingAssembly();
var allNames = srcAssembly.GetManifestResourceNames();
return srcAssembly.GetManifestResourceStream(pathName + "." + resName);
}
public static string GetResourceString(string pathName, string resName, Assembly srcAssembly = null)
{
if (srcAssembly == null) srcAssembly = Assembly.GetCallingAssembly();
StreamReader sr = new StreamReader(GetResourceStream(pathName, resName, srcAssembly));
string s = sr.ReadToEnd();
sr.Close();
return s;
}
The pathName is a bit tricky - it's the name of the project plus any folder names in your project. So if you have a project "MyApp" with a folder called "MyResources" with a file called "Batch.txt" marked as a resource, then you would access the contents with:
string contents = GetResourceString("MyApp.MyResources", "Batch.txt");
I have this solution structure:
I'd like to take a copy of this template and then save a copy of it to a network drive. Using System.IO I've previously using this code to take a copy of a file:
string templateFilePath = #"\\blah\blah\blah\blah\Temp.xlsx"; //<<<< X
string exportFilePath = #"\\blah\blah\blah\blah\Results.xlsx";
File.Copy(templateFilePath, exportFilePath, true);
Because the template is saved within the solution do I still need to specify the complete pathway or is there a shorter was of referencing this file?
You'll need to specify the full path of the file or the relative path in regards to where the executable is running. So you can set targetFileName = ".\template.xlsx
another way to get the file is to mark the Build Action in the properties of the file and set it to Embedded Resource. then use the following code to get the stream. Not sure if the stream will help you or not.
Assembly asm = Assembly.GetExecutingAssembly();
string file = string.Format("{0}.Template.xlsx", asm.GetName().Name);
var ms = new MemoryStream();
Stream fileStream = asm.GetManifestResourceStream(file);
I am using the SharpZip .NET Zip Library to unzip a file found in the Assets/MyZipFolder folder.
I need to get the full path so that I can use the following:
ZipInputStream s = new ZipInputStream(File.OpenRead(_zipFile))
How do I get the path to Assets/MyZipFolder/MyZip.zip to pass to a .NET File.OpenRead command?
From your Context you can simply open a read stream using:
using (var stream = Context.Assets.Open("MyZipFolder/MyZip.zip"))
{
var s = new ZipInputStream(stream);
// do read here ...
}
Be careful that the file is marked as an AndroidAsset for build action, the absolute path is: "file:///android_asset" and remember that file names in android are case sensitive.
How can I open files, which are embedded in a resource file, like a file on the harddisk (with an absolute path) ?
Suppose that you have the test.xml file embedded into the assembly. You could use the GetManifestResourceStream method to obtain a stream pointing towards the contents:
class Program
{
static void Main()
{
var assembly = Assembly.GetExecutingAssembly();
using (var stream = assembly.GetManifestResourceStream("ProjectName.test.xml"))
using (var reader = new StreamReader(stream))
{
Console.WriteLine(reader.ReadToEnd());
}
}
}
This way the contents of the file is read into memory. You can also store it to the harddisk and then access by absolute path but this might not be necessary as you already have the contents of the file.
What am I doing wrong in the following code?
public string ReadFromFile(string text)
{
string toReturn = "";
System.IO.FileStream stream = new System.IO.FileStream(text, System.IO.FileMode.Open);
System.IO.StreamReader reader = new System.IO.StreamReader(text);
toReturn = reader.ReadToEnd();
stream.Close();
return toReturn;
}
I put a text.txt file inside my bin\Debug folder and for some reason, each time when I enter this file name ("text.txt") I am getting an exception of System.IO.FileNotFoundException.
It is not safe to assume that the current working directory is identical to the directory in which your binary is residing. You can usually use code like the following to refer to the directory of your application:
string applicationDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
string filename = System.IO.Path.Combine(applicationDirectory, text);
This may or may not be a solution for your given problem. On a sidenote, text is not really a decent variable name for a filename.
If I want to open a file that is always in a folder relative to the application's startup path, I use:
Application.StartupPath
to simply get the startuppath, then I append the rest of the path (subfolders and or file name).
On a side note: in real life (i.e. in the end user's configuration) the location of a file you need to read is seldom relative to the applications startup path. Applications are usually installed in the Program Files folder, application data is stored elsewhere.
File.ReadAllText(path) does the same thing as your code. I would suggest using rooted path like "c:......\text.txt" instead of the relative path. The current directory is not necessarily set to your app's home directory.
You can use Process Monitor (successor to FileMon) to find out exactly what file your application tries to read.
My suggestions:
public string ReadFromFile(string fileName)
{
using(System.IO.FileStream stream = new System.IO.FileStream(fileName, System.IO.FileMode.Open))
using(System.IO.StreamReader reader = new System.IO.StreamReader(stream))
{
return = reader.ReadToEnd();
}
}
or even
string text = File.OpenText(fileName).ReadToEnd();
You can also check is file exists:
if(File.Exists(fileName))
{
// do something...
}
At last - maybe your text.txt file is open by other process and it can't be read at this moment.