开发者

Extended PageSize Information in C#

I have to print a custom label to a thermal printer. I have everything setup and working with one exception: the rolls of labels have two labels per row, but the C# printing objects can't seem to see that.

When I query the PageSize information, it tells me that the label is 3.15" x 0.75". While this is true for the entire label, it doesn't give me any information about the size of each individual label, or the spacing between.

Digging into the driver ini files, there is a line that looks like PageSize84 = THT-6-423,3150,2,1开发者_Python百科500,750,150,125,1. All of the information I need seems to be listed in this line (2 columns, 1500 wide, 750 tall), I just have no idea how to access it from C#. I've been scouring the web today, and I've had no luck.

I could always hard code the information for now, but this doesn't future proof the code if manufacturing changes labels.


If you are trying to read an ini file to get the values you can use this.

IniFile.ReadIniValue("[Tag]", "Server", @"C:\my.ini");

#region Usings

using System.Text;
using System.Runtime.InteropServices;
#endregion

/// <summary>
/// Communicates with ini files
/// </summary>
public static class IniFile
{
    #region Declarations



    #endregion

    #region Constructor/Deconstructor

    /// <summary>
    /// Initializes a new instance of the <see cref="IniFile"/> class.
    /// </summary>
    static IniFile()
    {
    }

    #endregion

    #region Properties



    #endregion

    #region Win32_API

    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(
        string section,
        string key, string def,
        StringBuilder retVal,
        int size, string filePath);

    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool WritePrivateProfileString(string lpAppName,
       string lpKeyName, string lpString, string lpFileName);

    #endregion

    /// <summary>
    /// Reads the ini value.
    /// </summary>
    /// <param name="section">The section.</param>
    /// <param name="key">The key.</param>
    /// <param name="iniFilePath">The ini file path.</param>
    /// <returns>Value stored in key</returns>
    /// <exception cref="FileNotFoundException"></exception>
    public static string ReadIniValue(string section, string key, string iniFilePath)
    {
        if(!File.Exists(iniFilePath))
        {
            throw new FileNotFoundException();
        }

        const int size = 255;
        var buffer = new StringBuilder(size);
        var len = GetPrivateProfileString(section, key, string.Empty, buffer, size, iniFilePath);

        if (len > 0)
        {
            return buffer.ToString();
        }
        return string.Empty;
    }

    /// <summary>
    /// Writes the ini value.
    /// </summary>
    /// <param name="section">The section.</param>
    /// <param name="keyname">The keyname.</param>
    /// <param name="valueToWrite">The value to write.</param>
    /// <param name="iniFilePath">The ini file path.</param>
    /// <returns>true if write was successful, false otherwise</returns>
    public static bool WriteIniValue(string section,string keyname,string valueToWrite,string iniFilePath)
    {
        return WritePrivateProfileString(section, keyname, valueToWrite, iniFilePath);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜