In the following code how to fix problem that the batch file won't get created. When I run in debug mode exception I get is "could not find part of path"

Thanks

StreamWriter sw = null;

            try
            {
                if (AgilentNVisaDriver.Checked == false && RequiredDlls.Checked == false)
                {
                    MessageBox.Show("Please select the application you would like to install");
                }
                else
                {
                    Assembly objAssembly = Assembly.GetAssembly(this.GetType());
                    string strAppPath = objAssembly.CodeBase;
                    strAppPath = strAppPath.Replace("SplashScreen.dll", "");
                    Uri objUri = new Uri(strAppPath);
                    strAppPath = @objUri.AbsolutePath;

                    string strBatchFilePath = strAppPath + @"Setup.bat";

                    sw = File.CreateText(strBatchFilePath);

Dani AI

Generated

The error "could not find part of the path" almost always means the directory portion of the path is wrong or the path string is malformed (not just that the file is missing). In your snippet you build a path from Assembly.CodeBase and a string replace of SplashScreen.dll; that approach can leave you with a URL-ish or encoded path (leading slash, %20 for spaces) or a folder that doesn't actually exist. As noted, long paths are also a frequent culprit — but start by verifying the actual path string first.

Safer pattern: get a proper filesystem folder, use Path.Combine, ensure the directory exists, and prefer Assembly.Location or AppDomain.CurrentDomain.BaseDirectory rather than fiddling with CodeBase. If you must use CodeBase, use new Uri(asm.CodeBase).LocalPath so percent-encoding is decoded. Example:

var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var batchPath = Path.Combine(dir, "Setup.bat");
Directory.CreateDirectory(Path.GetDirectoryName(batchPath)); // safe if already exists
File.WriteAllText(batchPath, "@echo off\r\nREM installer stub");

Quick debug checklist: 1) Log/MessageBox the computed path(s) you use. 2) Verify Directory.Exists(Path.GetDirectoryName(batchPath)). 3) Check Path.GetFullPath(batchPath).Length for MAX_PATH issues. 4) Try writing to a known-writable folder (Temp or AppData) to rule out permissions (Program Files/GAC require elevation). If after these checks you still see the exception, post the exact computed path and full exception text so someone can point out the precise failure.

You might be having a similar issue as described in this link.

Reply # 2 on that link outlines how to resolve that issue if that's the case.

It seems in the case of the link above the absolute path was so long (due to subdirectories and such) that it exceeded the maximum number of chars for the file path.

Hope this helps :) Please remember to mark as solved if your issue is resolved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.