Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将CSV写入ftp_C#_Web Services_Csv_Ftp - Fatal编程技术网

C# 将CSV写入ftp

C# 将CSV写入ftp,c#,web-services,csv,ftp,C#,Web Services,Csv,Ftp,我有一个Web服务,可以将数据编译在一起,我希望将其存储在CSV中,这样用户就可以获得下载链接来获取数据。这就是我所拥有的 string fileName = "Test.csv"; string ftpAddress = "MYDOMAIN.COM"; string username = "MYUSERNAME"; string password = "MYPASSWORD";

我有一个Web服务,可以将数据编译在一起,我希望将其存储在CSV中,这样用户就可以获得下载链接来获取数据。这就是我所拥有的

            string fileName = "Test.csv";
            string ftpAddress = "MYDOMAIN.COM";
            string username = "MYUSERNAME";
            string password = "MYPASSWORD";

            List<string[]> done = new List<string[]>();
            string[] firstline = new string[2];

            firstline[0] = "hello this is ";
            firstline[1] = "a test";
            done.Add(firstline);
            string[] secondline = new string[2];

            secondline[0] = "hello this is a ";
            secondline[1] = "second test";
            done.Add(secondline);
            string delimiter = ",";
            StringBuilder sb = new StringBuilder();


            for (int index = 0; index < 2; index++)
                sb.AppendLine(string.Join(delimiter, done[index]));

            string str = sb.ToString();
            byte[] buffer = new byte[str.Length * sizeof(char)];
            System.Buffer.BlockCopy(str.ToCharArray(), 0, buffer, 0, buffer.Length);



                WebRequest request = WebRequest.Create("ftp://" + ftpAddress + fileName);
                request.ContentType = 

                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.Credentials = new NetworkCredential(username, password);
                Stream reqStream = request.GetRequestStream();
                reqStream.Write(buffer, 0, buffer.Length);


                reqStream.Close();
string fileName=“Test.csv”;
字符串ftpAddress=“MYDOMAIN.COM”;
字符串username=“MYUSERNAME”;
字符串password=“MYPASSWORD”;
列表完成=新列表();
字符串[]第一行=新字符串[2];
第一行[0]=“你好,我是”;
第一行[1]=“测试”;
完成。添加(第一行);
字符串[]第二行=新字符串[2];
第二行[0]=“您好,这是一个”;
第二行[1]=“第二次测试”;
完成。添加(第二行);
字符串分隔符=“,”;
StringBuilder sb=新的StringBuilder();
对于(int-index=0;index<2;index++)
sb.AppendLine(string.Join(分隔符,done[index]);
字符串str=sb.ToString();
byte[]buffer=新字节[str.Length*sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(),0,Buffer,0,Buffer.Length);
WebRequest=WebRequest.Create(“ftp://”+ftpAddress+fileName);
request.ContentType=
request.Method=WebRequestMethods.Ftp.UploadFile;
request.Credentials=新的网络凭据(用户名、密码);
Stream reqStream=request.GetRequestStream();
请求流写入(缓冲区,0,缓冲区长度);
reqStream.Close();
CSV保存正确,但我没有得到它读取的单独列

“您好,这是一个测试”
“您好,这是第二个测试”

不如

“你好,这是”“一次测试”
“你好,这是”“第二次测试”


关于如何用逗号分隔列,有什么想法吗?

我已更改了您的代码以符合您的要求:

        string fileName = "Test.csv";
        string ftpAddress = "MYDOMAIN.COM";
        string username = "MYUSERNAME";
        string password = "MYPASSWORD";
        

        List<string[]> done = new List<string[]>();
        string[] firstline = new string[2];

        firstline[0] = "hello this is ";
        firstline[1] = "a test";
        done.Add(firstline);
        string[] secondline = new string[2];

        secondline[0] = "hello this is a ";
        secondline[1] = "second test";
        done.Add(secondline);
        string delimiter = @""",""";
        StringBuilder sb = new StringBuilder();

        foreach (var lineArray in done)           
            sb.AppendLine(string.Format(@"""{0}""", string.Join(delimiter, lineArray)));

        string str = sb.ToString();
        byte[] buffer = new byte[str.Length * sizeof(char)];
        System.Buffer.BlockCopy(str.ToCharArray(), 0, buffer, 0, buffer.Length);

        WebRequest request = WebRequest.Create("ftp://" + ftpAddress + fileName);
        request.ContentType =

        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential(username, password);
        Stream reqStream = request.GetRequestStream();
        reqStream.Write(buffer, 0, buffer.Length);

        reqStream.Close();

那么,在你的代码中,你希望它把引号放在哪里呢?我不想要引号,但是第一行都在单元格a1中,而不是在a1和a2之间分割。我糟糕的解释方式集中在你想在档案里写什么。毕竟,这只是一个文本文件。一旦您确定了文本文件在您使用的任何文件中正确打开所需的外观,那么让代码编写适当的文本文件就很简单了。也不清楚为什么您认为FTP部分是相关的。不要使用索引<2。例如,使用Count。除此之外,在第一行[0]和第二行[0]中,可以根据您的代码和解释放置逗号;
string delimiter = @""";""";