Jak czytać osadzony plik tekstowy zasobów

Jak odczytać osadzony zasób (plik tekstowy) za pomocą StreamReader i zwrócić go jako ciąg znaków? Mój obecny skrypt wykorzystuje formularz i pole tekstowe systemu Windows, które pozwala użytkownikowi znaleźć i zastąpić tekst w pliku tekstowym, który nie jest osadzony.

private void button1_Click(object sender, EventArgs e)
{
    StringCollection strValuesToSearch = new StringCollection();
    strValuesToSearch.Add("Apple");
    string stringToReplace;
    stringToReplace = textBox1.Text;

    StreamReader FileReader = new StreamReader(@"C:\MyFile.txt");
    string FileContents;
    FileContents = FileReader.ReadToEnd();
    FileReader.Close();
    foreach (string s in strValuesToSearch)
    {
        if (FileContents.Contains(s))
            FileContents = FileContents.Replace(s, stringToReplace);
    }
    StreamWriter FileWriter = new StreamWriter(@"MyFile.txt");
    FileWriter.Write(FileContents);
    FileWriter.Close();
}
Author: Alex Jolig, 2010-07-23

15 answers

Możesz użyć Assembly.GetManifestResourceStream metoda :

  1. Dodaj następujące zastosowania

    using System.IO;
    using System.Reflection;
    
  2. Ustaw właściwość odpowiedniego pliku:
    Parametr Build Action z wartością Embedded Resource

  3. Użyj następującego kodu

var assembly = Assembly.GetExecutingAssembly();
var resourceName = "MyCompany.MyProduct.MyFile.txt";

using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
    string result = reader.ReadToEnd();
}

resourceName jest nazwą jednego z zasobów wbudowanych w assembly. Na przykład, jeśli osadzono plik tekstowy o nazwie "MyFile.txt", który jest umieszczony w katalogu głównym projektu z domyślną przestrzenią nazw "MyCompany.MyProduct", to resourceName jest "MyCompany.MyProduct.MyFile.txt". Możesz uzyskać listę wszystkich zasobów w złożeniu za pomocą Assembly.GetManifestResourceNames metoda .

 929
Author: dtb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-04 10:15:25

Możesz dodać plik jako zasób za pomocą dwóch oddzielnych metod.

Kod C# wymagany do uzyskania dostępu do pliku jest inny , w zależności od metody użytej do dodania pliku.

Metoda 1: Dodaj istniejący plik, ustaw właściwość na Embedded Resource

Dodaj plik do projektu, a następnie Ustaw typ na Embedded Resource.

UWAGA: jeśli dodasz plik za pomocą tej metody, możesz użyć GetManifestResourceStream, aby uzyskać do niego dostęp (zobacz odpowiedź z @dtb).

Tutaj wpisz opis obrazka

Metoda 2: Dodaj plik do Resources.resx

Otwórz plik Resources.resx, Użyj rozwijanego pola, aby dodać plik, Ustaw Access Modifier na public.

UWAGA: jeśli dodasz plik za pomocą tej metody, możesz użyć Properties.Resources, aby uzyskać do niego dostęp(zobacz odpowiedź z @ Night Walker).

Tutaj wpisz opis obrazka

 110
Author: Contango,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-27 10:10:40

Spójrz na tę stronę: http://support.microsoft.com/kb/319292

Zasadniczo, używasz System.Reflection, Aby uzyskać odniesienie do bieżącego zestawu. Następnie używasz GetManifestResourceStream().

Przykład, ze strony, którą zamieściłem:

Uwaga : trzeba dodać using System.Reflection; aby to działało

   Assembly _assembly;
   StreamReader _textStreamReader;

   try
   {
      _assembly = Assembly.GetExecutingAssembly();
      _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("MyNamespace.MyTextFile.txt"));
   }
   catch
   {
      MessageBox.Show("Error accessing resources!");
   }
 84
Author: Chris Laplante,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-07-22 23:01:24

W Visual Studio możesz bezpośrednio osadzić dostęp do zasobu plików za pomocą zakładki zasoby we właściwościach projektu ("Analytics" w tym przykładzie). Visual studio screen shot-Zakładka zasoby

Plik wynikowy może być następnie dostępny jako tablica bajtów przez

byte[] jsonSecrets = GoogleAnalyticsExtractor.Properties.Resources.client_secrets_reporter;

Jeśli potrzebujesz go jako strumienia, to ( z https://stackoverflow.com/a/4736185/432976 )

Stream stream = new MemoryStream(jsonSecrets)
 65
Author: Andrew Hill,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:02:56

Po dodaniu pliku do zasobów, należy wybrać modyfikatory dostępu jako publiczne, niż można zrobić coś takiego jak poniżej.

byte[] clistAsByteArray = Properties.Resources.CLIST01;

CLIST01 to nazwa osadzonego pliku.

W zasadzie możesz przejść do zasobów.Projektant.cs i zobacz jak nazywa się getter.
 26
Author: Night Walker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-12 20:42:27

Wiem, że to stary wątek, ale to mi się udało:

  1. Dodaj plik tekstowy do zasobów projektu
  2. Ustaw modyfikator dostępu na publiczny, jak pokazano powyżej przez Andrew Hilla
  3. Przeczytaj tekst w ten sposób:

    textBox1 = new TextBox();
    textBox1.Text = Properties.Resources.SomeText;
    

Tekst, który dodałem do zasobów: 'SomeText.txt "

 12
Author: S_Teo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-30 14:29:46

Możesz również użyć tej uproszczonej wersji odpowiedzi @dtb:

public string GetEmbeddedResource(string ns, string res)
{
    using (var reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(string.Format("{0}.{1}", ns, res))))
    {
        return reader.ReadToEnd();
    }
}
 7
Author: Timmerz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-08 19:14:35

Dowiedziałem się właśnie, że Twój plik nie może mieć"."(kropka) w nazwie pliku.

A "."nazwa pliku nie jest dobra.

Szablony.plainEmailBodyTemplate-en.txt -- > działa!!!
Szablony.plainEmailBodyTemplate.en.txt -- > nie działa przez GetManifestResourceStream ()

Prawdopodobnie dlatego, że framework się myli między przestrzeniami nazw a nazwami plików...

 7
Author: Peter Gfader,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-03 17:28:58

Dodanie np. pliku testowego.sql Menu projektu -> właściwości -> zasoby - > Dodaj istniejący plik

    string queryFromResourceFile = Properties.Resources.Testfile.ToString();
 5
Author: miciry89,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-14 19:25:28

Czytam plik tekstowy osadzonego zasobu:

    /// <summary>
    /// Converts to generic list a byte array
    /// </summary>
    /// <param name="content">byte array (embedded resource)</param>
    /// <returns>generic list of strings</returns>
    private List<string> GetLines(byte[] content)
    {
        string s = Encoding.Default.GetString(content, 0, content.Length - 1);
        return new List<string>(s.Split(new[] { Environment.NewLine }, StringSplitOptions.None));
    }

Próbka:

var template = GetLines(Properties.Resources.LasTemplate /* resource name */);

template.ForEach(ln =>
{
    Debug.WriteLine(ln);
});
 4
Author: harveyt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-23 08:34:58

By all your powers summared I use this helper class for reading resources from any assembly and any namespace in a generic way.

public class ResourceReader
{
    public static IEnumerable<string> FindEmbededResources<TAssembly>(Func<string, bool> predicate)
    {
        if (predicate == null) throw new ArgumentNullException(nameof(predicate));

        return
            GetEmbededResourceNames<TAssembly>()
                .Where(predicate)
                .Select(name => ReadEmbededResource(typeof(TAssembly), name))
                .Where(x => !string.IsNullOrEmpty(x));
    }

    public static IEnumerable<string> GetEmbededResourceNames<TAssembly>()
    {
        var assembly = Assembly.GetAssembly(typeof(TAssembly));
        return assembly.GetManifestResourceNames();
    }

    public static string ReadEmbededResource<TAssembly, TNamespace>(string name)
    {
        if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name));
        return ReadEmbededResource(typeof(TAssembly), typeof(TNamespace), name);
    }

    public static string ReadEmbededResource(Type assemblyType, Type namespaceType, string name)
    {
        if (assemblyType == null) throw new ArgumentNullException(nameof(assemblyType));
        if (namespaceType == null) throw new ArgumentNullException(nameof(namespaceType));
        if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name));

        return ReadEmbededResource(assemblyType, $"{namespaceType.Namespace}.{name}");
    }

    public static string ReadEmbededResource(Type assemblyType, string name)
    {
        if (assemblyType == null) throw new ArgumentNullException(nameof(assemblyType));
        if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name));

        var assembly = Assembly.GetAssembly(assemblyType);
        using (var resourceStream = assembly.GetManifestResourceStream(name))
        {
            if (resourceStream == null) return null;
            using (var streamReader = new StreamReader(resourceStream))
            {
                return streamReader.ReadToEnd();
            }
        }
    }
}
 4
Author: t3chb0t,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-11 17:09:39

Wiem, że to stare, ale chciałem tylko zwrócić uwagę na NETMF (. Net MicroFramework), można to łatwo zrobić:

string response = Resources.GetString(Resources.StringResources.MyFileName);

Od NETMF nie posiada GetManifestResourceStream

 3
Author: dreamwork801,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-17 06:50:11

Byłem zirytowany, że trzeba zawsze zawierać przestrzeń nazw i folder w łańcuchu. Chciałem uprościć dostęp do wbudowanych zasobów. Dlatego napisałem tę małą lekcję. Zapraszam do korzystania i poprawy!

Użycie:

using(Stream stream = EmbeddedResources.ExecutingResources.GetStream("filename.txt"))
{
 //...
}

Klasa:

public class EmbeddedResources
{
    private static readonly Lazy<EmbeddedResources> _callingResources = new Lazy<EmbeddedResources>(() => new EmbeddedResources(Assembly.GetCallingAssembly()));

    private static readonly Lazy<EmbeddedResources> _entryResources = new Lazy<EmbeddedResources>(() => new EmbeddedResources(Assembly.GetEntryAssembly()));

    private static readonly Lazy<EmbeddedResources> _executingResources = new Lazy<EmbeddedResources>(() => new EmbeddedResources(Assembly.GetExecutingAssembly()));

    private readonly Assembly _assembly;

    private readonly string[] _resources;

    public EmbeddedResources(Assembly assembly)
    {
        _assembly = assembly;
        _resources = assembly.GetManifestResourceNames();
    }

    public static EmbeddedResources CallingResources => _callingResources.Value;

    public static EmbeddedResources EntryResources => _entryResources.Value;

    public static EmbeddedResources ExecutingResources => _executingResources.Value;

    public Stream GetStream(string resName) => _assembly.GetManifestResourceStream(_resources.Single(s => s.Contains(resName)));

}
 3
Author: Felix Keil,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-13 15:25:54

Po przeczytaniu wszystkich rozwiązań zamieszczonych tutaj. Tak to rozwiązałem:

// How to embedded a "Text file" inside of a C# project
//   and read it as a resource from c# code:
//
// (1) Add Text File to Project.  example: 'myfile.txt'
//
// (2) Change Text File Properties:
//      Build-action: EmbeddedResource
//      Logical-name: myfile.txt      
//          (note only 1 dot permitted in filename)
//
// (3) from c# get the string for the entire embedded file as follows:
//
//     string myfile = GetEmbeddedResourceFile("myfile.txt");

public static string GetEmbeddedResourceFile(string filename) {
    var a = System.Reflection.Assembly.GetExecutingAssembly();
    using (var s = a.GetManifestResourceStream(filename))
    using (var r = new System.IO.StreamReader(s))
    {
        string result = r.ReadToEnd();
        return result;
    }
    return "";      
}
 2
Author: Bill Moore,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-26 22:49:11

Odczyt osadzonego pliku TXT przy zdarzeniu ładowania formularza.

Ustawia zmienne dynamicznie.

string f1 = "AppName.File1.Ext";
string f2 = "AppName.File2.Ext";
string f3 = "AppName.File3.Ext";

Spróbuj złapać.

try 
{
     IncludeText(f1,f2,f3); 
     /// Pass the Resources Dynamically 
     /// through the call stack.
}

catch (Exception Ex)
{
     MessageBox.Show(Ex.Message);  
     /// Error for if the Stream is Null.
}

Utwórz Void dla IncludeText (), Visual Studio zrobi to za Ciebie. Kliknij żarówkę, aby automatycznie wygenerować blokadę kodu.

w wygenerowanym bloku kodu umieść następujące elementy

Zasób 1

var assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(file1))
using (StreamReader reader = new StreamReader(stream))
{
string result1 = reader.ReadToEnd();
richTextBox1.AppendText(result1 + Environment.NewLine + Environment.NewLine );
}

Zasób 2

var assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(file2))
using (StreamReader reader = new StreamReader(stream))
{
string result2 = reader.ReadToEnd();
richTextBox1.AppendText(
result2 + Environment.NewLine + 
Environment.NewLine );
}

Zasób 3

var assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(file3))

using (StreamReader reader = new StreamReader(stream))
{
    string result3 = reader.ReadToEnd();
    richTextBox1.AppendText(result3);
}

Jeśli chcesz wysłać zwracaną zmienną gdzie indziej, po prostu wywołanie innej funkcji i...

using (StreamReader reader = new StreamReader(stream))
{
    string result3 = reader.ReadToEnd();
    ///richTextBox1.AppendText(result3);
    string extVar = result3;

    /// another try catch here.

   try {

   SendVariableToLocation(extVar)
   {
         //// Put Code Here.
   }

       }

  catch (Exception ex)
  {
    Messagebox.Show(ex.Message);
  }

}

To, co udało się osiągnąć, to metoda łączenia wielu plików txt i odczytywania ich osadzonych danych wewnątrz pojedynczego pola tekstowego. co było moim pożądanym efektem z tą próbką kodu.

 0
Author: MasterCassidy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-07 10:33:23