如何通过C#或其他途径带凭据启动Tally ERP 9?
Hey there! I’ve worked with Tally ERP 9 automation via C# quite a bit, so I can walk you through a few practical ways to launch Tally with credentials programmatically. Let’s break down the most reliable approaches:
Tally ERP 9 supports passing username and password directly via command line arguments, which lets you skip the manual login prompt entirely. You can use C#'s Process.Start() to trigger the Tally executable with these parameters.
Here’s a working code example:
using System.Diagnostics; // Update these paths/values to match your environment var tallyExePath = @"C:\Program Files\Tally.ERP9\tally.exe"; var tallyUsername = "your_tally_login_id"; var tallyPassword = "your_tally_password"; var targetCompanyPath = @"C:\TallyData\YourCompanyFile.Tally"; // Optional: Auto-open a specific company // Build the argument string var launchArgs = $"/USERNAME:{tallyUsername} /PASSWORD:{tallyPassword}"; if (!string.IsNullOrEmpty(targetCompanyPath)) { launchArgs += $" /COMPANY:{targetCompanyPath}"; } // Configure and start the process var startInfo = new ProcessStartInfo { FileName = tallyExePath, Arguments = launchArgs, UseShellExecute = false, // Required to pass arguments correctly CreateNoWindow = false // Set to true if you want to launch Tally in background }; Process.Start(startInfo);
⚠️ Important note: This method exposes the password as plain text in process arguments, so only use this for local, secure environments. Avoid it for shared or production systems.
Since you’re already pushing data to Tally via C#, you’re probably familiar with its XML API. You can first validate your credentials through an API call, then launch Tally—if your Tally configuration allows session sharing, it’ll auto-use the validated session.
Step 1: Send a validation XML request
Use HttpClient or HttpWebRequest to send this XML to Tally’s default port (9000):
<ENVELOPE> <HEADER> <TALLYREQUEST>VALIDATEUSER</TALLYREQUEST> </HEADER> <BODY> <USERNAME>your_tally_username</USERNAME> <PASSWORD>your_tally_password</PASSWORD> <COMPANYNAME>Your Company Official Name</COMPANYNAME> </BODY> </ENVELOPE>
If validation succeeds, Tally will return a response with <STATUS>SUCCESS</STATUS>.
Step 2: Launch Tally normally
Once the session is validated, simply start tally.exe without credentials—it’ll pick up the active authenticated session automatically (assuming Tally’s settings allow this).
To avoid plaintext passwords in your code or process arguments, store Tally’s credentials in Windows Credential Manager, then fetch them programmatically in C# before launching Tally.
Here’s how to read stored credentials:
using System; using System.Runtime.InteropServices; public static class TallyCredentialHelper { [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)] private static extern bool CredRead(string target, int type, int reservedFlag, out IntPtr credentialPtr); [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)] private static extern void CredFree(IntPtr credentialPtr); public static (string Username, string Password) GetTallyCredentials(string credentialTargetName) { IntPtr credPtr = IntPtr.Zero; try { // CRED_TYPE_GENERIC = 1 if (CredRead(credentialTargetName, 1, 0, out credPtr)) { var credential = Marshal.PtrToStructure<CREDENTIAL>(credPtr); return (credential.UserName, Marshal.PtrToStringUni(credential.CredentialBlob)); } throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); } finally { if (credPtr != IntPtr.Zero) CredFree(credPtr); } } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct CREDENTIAL { public int Flags; public int Type; public string TargetName; public string Comment; public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten; public int CredentialBlobSize; public IntPtr CredentialBlob; public int Persist; public int AttributeCount; public IntPtr Attributes; public string TargetAlias; public string UserName; } } // Usage var tallyCreds = TallyCredentialHelper.GetTallyCredentials("TallyERP9_YourCompany"); // Now use tallyCreds.Username and tallyCreds.Password with Method 1 or Method 2
This keeps your credentials encrypted in Windows’ secure store, eliminating plaintext exposure.
- Verify your Tally ERP 9 version supports command line parameters (most modern versions do, but test with your build)
- For network Tally installations, ensure your client has network access to the server and your credentials have the right permissions
- If Tally has forced login restrictions enabled, you may need to adjust its configuration to allow programmatic authentication
- Command line parameters are case-sensitive—stick to the exact format (e.g.,
/USERNAME:not/username:)
内容的提问来源于stack exchange,提问作者moses

