내 루프를 반복하면서 내 datagridview에 출력을 보내려고합니다.DataGridView 문제
DataGridView에 3 개 정의 된 C 럼이 경로, 오래된 파일 이름, 새 파일 이름
그러나, 나는 타격 계속 오류를. 그래서 난이 정확하지 심지어 100 % 확신
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# 새로운 해요 :
첫째, 여기에 코드입니다. 이 코드가하는 일은 다른 메서드에서 전달 된 경로를 사용하고 잘못된 문자 (예 : 정규식 패턴에 정의 된대로)를 제거하여 파일 이름을 "정리"합니다. 파일 이름이 이미 있으면 파일 이름에 점진적으로 증가하는 숫자를 추가하십시오 (예 : ~ test.txt 및 % test.txt는 모두 내 코드로 test.txt로 이름이 바뀌고 ~ test.txt는 test.txt가되고 % test.txt는 test1.txt가됩니다. 나는 (파일이 다른 경우 (filesCount.ContainsKey (소독) 루프로 이동한다) 내 DataGridView에이 데이터를 추가하기 위해 기본적으로 노력하고있어.
내가 Path.GetFileNameWithoutExtension(), Path.GetExtension (와 오류를 얻을 .) Path.Combine()는 Path.GetDIrectoryName()는 에러가 다음과 같다 :
'System.Windows.Forms.DataGridViewTextBoxColumn' 가 '하는 getFilename'없이 확장 방법에 대한 정의를 포함하지 않는다 'GetFileName' 형식의 인수 을 받아들입니다. 'System.Windows.Forms.DataGridViewTextBoxColumn',451,515,가 볼 수 있습니다 (당신은 using 지시문 또는 어셈블리 참조 누락?)
나는 아무 생각이 무엇을 의미하는지 또는이 문제를 해결하는 방법에 대해 설명합니다. 누군가 도울 수 있습니까?
게시 한 코드에서 컴파일 오류가 발생하는 곳을 볼 수 없습니다. 오류가 발생하는 행을 나타낼 수 있습니까? – aqwert
위의 코드를 기반으로 오류가 어떻게 발생하는지 알 수 없습니다. 그러나 제가 물어 보도록하겠습니다. 데이터 그리드를 사용해야하는 이유가 있습니까? 행과 열을 수동으로 생성하고 아무것도 바인딩하지 않으려는 경우을 사용하는 것이 더 간단 할 수 있습니다. 그것이 효과가 있다고 생각하면 그에 대한 샘플 코드를 제공해 드릴 수 있습니다. –
CodingGorilla