我正在尝试遍历我的foreach循环并将输出吐出到我的datagridview。
datagridview有3个已定义的列:路径,旧文件名,新文件名
然而,我一直遇到错误。
首先,这是代码:
public partial class SanitizeFileNames : Form
{
public SanitizeFileNames()
{
InitializeComponent();
}
public void Sanitizer(List<string> paths)
{
string regPattern = (@"[~#&!%+{}]+");
string replacement = "";
Regex regExPattern = new Regex(regPattern);
List<string> existingNames = new List<string>();
StreamWriter errors = new StreamWriter(@"C:\Documents and Settings\bob.smith\Desktop\SharePointTesting\Errors.txt", true);
StreamWriter resultsofRename = new StreamWriter(@"C:\Documents and Settings\bob.smith\Desktop\SharePointTesting\Results of File Rename.txt", true);
dataGridView1.Rows.Clear();
var filesCount = new Dictionary<string, int>();
try
{
foreach (string files2 in paths)
{
string filenameOnly = Path.GetFileName(files2);
string pathOnly = Path.GetDirectoryName(files2);
string sanitizedFileName = regExPattern.Replace(filenameOnly, replacement);
string sanitized = Path.Combine(pathOnly, sanitizedFileName);
if (!System.IO.File.Exists(sanitized))
{
System.IO.File.Move(files2, sanitized);
DataGridViewRow dgr2 = new DataGridViewRow();
dgr2.Cells[0].Value = pathOnly;
dgr2.Cells[1].Value = filenameOnly;
dgr2.Cells[2].Value = sanitized;
resultsofRename.Write("Path: " + pathOnly + "\r\n" + "Old File Name: " + filenameOnly + "\r\n" + "New File Name: " + sanitized + "\r\n" + "\r\n");
}
else
{
if (filesCount.ContainsKey(sanitized))
{
filesCount[sanitized]++;
}
else
{
filesCount.Add(sanitized, 1);
}
string newFileName = String.Format("{0}{1}{2}",
Path.GetFileNameWithoutExtension(sanitized),
filesCount[sanitized].ToString(),
Path.GetExtension(sanitized));
string newFilePath = Path.Combine(Path.GetDirectoryName(sanitized), newFileName);
System.IO.File.Move(files2, newFilePath);
DataGridViewRow dgr2 = new DataGridViewRow();
dgr2.Cells[0].Value = pathOnly;
dgr2.Cells[1].Value = filenameOnly;
dgr2.Cells[2].Value = newFileName;
resultsofRename.Write("Path: " + pathOnly + "\r\n" + "Old File Name: " + Path.GetFileNameWithoutExtension(files2) + "\r\n" + "New File Name: " + newFileName + "\r\n" + "\r\n");
}
}
}
catch (Exception e)
{
errors.Write(e);
}
}
private void SanitizeFileNames_Load(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
首先,我是C#的新手,所以我甚至不能100%确定这是正确的。这段代码的作用是获取从另一个方法传入的路径,并通过删除无效字符(在我的正则表达式模式中定义)来“清理”文件名。如果文件名已经存在,请附加一个增量增加的数字文件名(即〜test.txt和%test.txt都将重命名为test.txt。使用我的代码,~test.txt变为test.txt和% test.txt成为test1.txt)。 我正在尝试将这些数据附加到我的datagridview(如果文件转到else if(filesCount.ContainsKey(sanitized)循环)。
我在Path.GetFileNameWithoutExtension(),Path.GetExtension(),Path.Combine(),Path.GetDIrectoryName()中遇到错误。错误如下:
'System.Windows.Forms.DataGridViewTextBoxColumn' 不包含的定义 'GetFileName'并没有扩展方法 'GetFileName'接受第一个 类型的论证 'System.Windows.Forms.DataGridViewTextBoxColumn' 可以找到(你错过了吗? 使用指令或程序集 引用?)
我不知道这意味着什么或如何解决这个问题。有人可以帮忙吗?
答案 0 :(得分:3)
我怀疑你有一个名为'Path'的DataGridViewTextBoxColumn变量/字段。重命名该变量和/或使用全名System.IO.Path.GetFileName()
等。
作为更一般的说法,请停止使用DatagridView作为数据结构。帮自己一个忙,用你想要的(列)属性定义一个类,声明一个BindingList<MyFileClass>
并将DataGridView绑定到该列表。
答案 1 :(得分:0)
示例:
public ArrayList GetSelectedRows(DataGrid datagrid)
{
ArrayList arrSelectedRows = new ArrayList();
DataSet dset = (DataSet)datagrid.DataSource;
for (int i=0; i<dset.Tables[0].Rows.Count; i++)
{
if (datagrid.IsSelected(i))
{
DataRow drow = dset.Tables[0].Rows[i];
arrSelectedRows.Add(drow);
}
}
return arrSelectedRows;
}
这应该让你想要去......但是,显然不像你之后的那样。
This should get you were you want to go...though, obviously not quite like what you were after either.