简体   繁体   中英

Why .bat file works and C# code doesn't work for folder mapping

I have written a code in C# to map a Drobo Drive to my machine, which doesn't work. but similar code lines used from bat file works and maps.

I am wondering if there is a difference in C# code and .bat file code which allows the code to work for mapping.

In the past I have also wrote code in C# that didn't work for moving files from Drobo, but a .bat file code worked.

Code from C#:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MapDrive
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // Map Network drive
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();

                psi.FileName = "cmd.exe";

                psi.Arguments = @"/C net use N: \\DroboNS2\Folder /USER:credentials /PERSISTENT:YES";

                psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
                process.StartInfo = psi;
                process.Start();
                process.WaitForExit();
                process.Close();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Error in mapping. Stack Trace: " + ex.ToString());
            }
        }
    }
}

VB Code:

@echo off
net use N: \\DroboNS2\Folder /USER:Credentials /persistent:yes

Both the files (.exe and .bat) are put in the Startup folder and tested.

This will work.

using (Process process = Process.Start("net.exe", @"use N: \\DroboNS2\Folder /USER:credentials /PERSISTENT:YES"))
    process.WaitForExit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM