FileStream fs = new (3个参数)我估计是后两个参数的问题

解决方案 »

  1.   

    1.drag图片时: 
     private void panel1_DragDrop(object sender, DragEventArgs e)
            {
                try
                {
                    new FileIOPermission(PermissionState.Unrestricted).Assert();
                    //   StringBuilder fileTable = new StringBuilder();
                    foreach (string fileListItem in (e.Data.GetData(DataFormats.FileDrop) as string[]))
                    {
                        Bitmap bm = new Bitmap(fileListItem);
                        //get image name
                        int pos = fileListItem.LastIndexOf(@"\") + 1;
                        string name = fileListItem.Substring(pos); ;
                        str = str + 1;
                        Drag darg = new Drag(bm, name, str-2, ds);
                        darg.Left = x * 175 + 15 * (x + 1) + panel1.AutoScrollPosition.X;
                        darg.Top = y * 150 + 10 * (y + 1) + panel1.AutoScrollPosition.Y;
                        darg.Width = 175;
                        darg.Height = 150;
                        //imgList[x] = fileListItem;
                        
                        dr = ds.Tables[0].NewRow();
                        dr[0] = x;
                        dr[1] = fileListItem;
                        ds.Tables[0].Rows.Add(dr);
                     
                        this.panel1.Controls.Add(darg);                    x += 1;
                        if (x % 3 == 0)
                        {
                            x = 0;
                            y += 1;                    }                }              
                    CodeAccessPermission.RevertAssert();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }2。上传图片时
       private void CopyWithProgress(string[] filenames)
            {
                
             
                // Display the ProgressBar control.
                this.progressBar1.Visible = true;
                // Set Minimum to 1 to represent the first file being copied.
                progressBar1.Minimum = 1;
                // Set Maximum to the total number of files to copy.
                progressBar1.Maximum = filenames.Length;
              //  MessageBox.Show(filenames.Length.ToString());
                // Set the initial value of the ProgressBar.
                progressBar1.Value = 1;
                // Set the Step property to a value of 1 to represent each file being copied.
                progressBar1.Step = 1;
               
                for (int x = 0; x < filenames.Length; x++)
                {
                   
                    int pos = filenames[x].LastIndexOf(@"\") + 1;
                    
                    string filename = filenames[x].Substring(pos); 
                    this.labFilename.Text = filenames[x];
                   //这里出现了问题,
                    FileStream fs = File.Create(filename[x].ToString());
    //这里出现了问题
                    byte[] info=new byte[fs.Length];
                    fs.Close();
                    if (uploadService.UploadFiles(info, @"img\" + filename))
                    {
                       // MessageBox.Show("Upload Images");
                    }
                    else
                    {
                        MessageBox.Show("No Upload Images");                }
                  
                 
                    progressBar1.PerformStep();
                    Bitmap bm = new Bitmap(filenames[x]);
                    this.pictureBox1.Image = bm;
                }        }
      

  2.   

    请问 new FileIOPermission(PermissionState.Unrestricted).Assert();这行是什么作用?而这里未指明要访问哪个路径啊?
    我写了个类,将文件上传到服务器上,在本机上可以正常,但服务器上则不行,提示错误:
    Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.网上找了关于安全的资料,没找到相关的说法,有没有高手这方面的资料?
      

  3.   

     Bitmap   bm   =   new   Bitmap(fileListItem); 
    这一句从文件创建位图实际已经独占了了文件。
    可以改成这样。
    using (Bitmap bit = new Bitmap(fileListItem))
    { this.panel1.BackgroundImage = bit.GetThumbnailImage(panel1.Width, panel1.Height, null, IntPtr.Zero);
    }
    另外你的文件上传代码中
    FileStream   fs   =   File.Create(filename[x].ToString()); 
    //这里出现了问题 
                                    byte[]   info=new   byte[fs.Length]; 
                                 fs.Close(); 
    这几句似乎逻辑就有问题。文件已经存在了,为什么又要新建文件而不是打开读取,那文件长度字节不是成为0了吗?